Go to the first, previous, next, last section, table of contents.


Regexp Functions

It is often useful to construct regular expressions by concatenating several strings together; the problem with doing this is you may not know if a string contains any characters which the regexp compiler reacts specially to (i.e. `*', `|', ...). Obviously these characters should be protected by a backslash, the following function will do this for you.

Function: regexp-quote string
This function returns a new version of the string string, any characters in string which are regexp meta-characters are quoted with a backslash.

If the string contains no meta-characters the original string is returned, without being copied.

(regexp-quote "foo*bar+baz")
    => "foo\\*bar\\+baz"

Note that in the above example the backslashes in the returned string are only single backslashes; the print functions print a single backslash character as `\\' so they can be read back in.

This function is usually used when a part of a regexp being constructed is unknown at compile time, often provided by the user.

As the section describing regexp syntax notes, the strings that parenthesised expressions match are recorded, the following two functions allow Lisp programs to access the positions of these strings.

Function: match-start &optional expression-index
This function returns the position which the parenthesised expression number expression-index started at in the last successful regexp match.

If expression-index is nil or zero the start of the whole string matched is returned instead.

The returned value will either be a position object if the last match was in a buffer, or an integer if the last match was in a string (i.e. regexp-match).

(regexp-match "foo(bar)" "xyzfoobarsaalsd")
    => t
(match-start)
    => 3
(match-start 1)
    => 6

Function: match-end &optional expression-index
Return the position which the parenthesised expression number expression-index ended at in the last successful regexp match.

If expression-index is nil or zero the end of the whole match is returned instead.

The returned value will either be a position object if the last match was in a buffer, or an integer if the last match was in a string (i.e. regexp-match).

(regexp-match "foo(bar)" "xyzfoobarsaalsd")
    => t
(match-end)
    => 9
(match-end 1)
    => 9


Go to the first, previous, next, last section, table of contents.