unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Kevin Rodgers <ihs_4664@yahoo.com>
Subject: Re: Regular expression search
Date: Wed, 01 Nov 2006 13:04:11 -0700	[thread overview]
Message-ID: <eiaul8$2qe$1@sea.gmane.org> (raw)
In-Reply-To: <200611011131.30744.help-gnu-emacs@vsbe.com>

vb wrote:
> let's say I need a function to find first printable character on the line 
> where the pointer is. This is what I'm trying to use:
> 
> (defun vb-first-printable ()
>   (interactive)
>   (let (limit-position)
>     (beginning-of-line)
>     (next-line 1)
>     (setq limit-position (point))
>     (previous-line 1)
>     (re-search-forward "\\S" limit-position 't)))
> 
> when I try executing this, I get the following error:
> 
> ====================================================
> Debugger entered--Lisp error: (invalid-regexp "Premature end of regular 
> expression")
>   re-search-forward("\\S" 3543 t)
>   (let (limit-position) (beginning-of-line) (next-line 1) (setq limit-position 
> (point)) (previous-line 1) (re-search-forward "\\S" limit-position (quote 
> t)))
>   vb-first-printable()
>   call-interactively(vb-first-printable)
> ===================================================
> 
> the same problem happens when I try re-search-forward from the command line: 
> if I enter "\S" as the pattern to search, I get "premature end of regular 
> expression" error,  but if I enter "\\S" as the regular expression pattern, 
> the only thing it finds is this pattern (\\S) itself (as I try it on the same 
> file where the source code is).
> 
> What am I missing here?

Commands that prompt you for a regexp allow you to enter it directly;
but when calling a Lisp function you have to specify the regexp as a
string, and in order to represent a backslash within a (double quote- 
delimited) string literal you must double it: "This string has 1 
backslash (here: \\) and 1 double quote (here: \")."  And of course
the `\\' regexp matches the backslash character itself.

The manual states:

,----
| `\sC'
|      matches any character whose syntax is C.  Here C is a character
|      that designates a particular syntax class: thus, `w' for word
|      constituent, `-' or ` ' for whitespace, `.' for ordinary
|      punctuation, etc.  *Note Syntax::.
|
| `\SC'
|      matches any character whose syntax is not C.
`----

So you must specify a syntax class `C', e.g. `w' for word constituent,
`-' or ` ' for whitespace, `.' for ordinary punctuation, etc.

But as there is no syntax class for printable or non-printable
characters, that seems like a dead end.  But there is the [:print:]
character class that you can use in regular expressions.

And finally, all that limit-position/next-line/point/previous-line stuff
can be replaced by line-end-position:

(defun vb-first-printable ()
   (interactive)
   (beginning-of-line)
   (re-search-forward "[[:print:]]" (line-end-position)))

-- 
Kevin

  reply	other threads:[~2006-11-01 20:04 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.7939.1160429872.9609.help-gnu-emacs@gnu.org>
2006-10-10 12:51 ` Where is clipper.el's homepage today? greg.bognar
2006-10-17  7:37 ` Mathias Dahl
2006-11-01 19:31   ` Regular expression search vb
2006-11-01 20:04     ` Kevin Rodgers [this message]
2006-11-01 21:10       ` vb
2006-11-01 22:23         ` Edward O'Connor
2006-11-01 23:08           ` vb
2006-11-02 18:49         ` Kevin Rodgers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='eiaul8$2qe$1@sea.gmane.org' \
    --to=ihs_4664@yahoo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).