[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Regular expression search
From: |
Kevin Rodgers |
Subject: |
Re: Regular expression search |
Date: |
Wed, 01 Nov 2006 13:04:11 -0700 |
User-agent: |
Thunderbird 1.5.0.7 (Windows/20060909) |
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