Previous: , Up: Regular Expressions   [Contents][Index]

5.30.2 Regexp Functions

These functions are exported by the rep.regexp module.

Function: quote-regexp string

Return a version of string, such that when used as a regexp, it will match the original contents of string verbatim, and nothing else. This involves quoting regexp meta-characters.

(quote-regexp "abc")
    ⇒ "abc"

(quote-regexp "a+c")
    ⇒ "a\\+c"
Function: string-match regexp string #!optional start ignore-case

Returns true if the string string matches the regular expression regexp. The string matches if executing the regexp at any position in the string succeeds.

When defined, start is the index of the first character to start matching at (counting from zero). When ignore-case is true the case of matched strings are ignored. Note that character classes are still case-significant.

(string-match "ab+c" "abbbc")
    ⇒ t

(string-match "ab+c" "xxxabbbcyyy")
    ⇒ t
Function: string-looking-at regexp string #!optional start ignore-case

Similar to string-match, but only returns true if string matches regexp starting at the character at index start in the string (or the first character if start is undefined).

(string-looking-at "ab+c" "abbbc" 0)
    ⇒ t

(string-looking-at "ab+c" "xxxabbbcyyy" 0)
    ⇒ ()

(string-looking-at "ab+c" "xxxabbbcyyy" 3)
    ⇒ t
Function: match-start #!optional n

Returns the position at which the n’th parenthesised expression started in the last successful regexp match. If n is false or zero the position of the start of the whole match is returned instead.

When matching strings, all positions are integers, with the first character in the string represented by zero. However, extensions that allow regexps to be matched against other textual inputs may return different position values.

(string-match "x*(foo|bar)y" "xxxbary")
    ⇒ t

(match-start 1)
    ⇒ 3
Function: match-end #!optional n

Similar to match-start, but returns the position of the character following the matched item.

(string-match "x*(foo|bar)y" "xxxbary")
    ⇒ t

(match-end 1)
    ⇒ 6

A common use of regular expressions is to match a string, then replace certain portions of the string with other text.

Function: expand-last-match template

Expand the template substituting the parenthesised expressions from the most recent successfully matched regular expression.

template may contain the following substitution-inducing escape sequences:


Substitute the whole string matched by the last regexp


Substitute the n’th parenthensised expression, where 1 <= N <= 9.


Substitute a single backslash character.

(string-match "x*(foo|bar)y" "xxxbary")
    ⇒ t

(expand-last-match "test-\\1-ing")
    ⇒ "test-bar-ing"

Note that double backslashes are required due to the read syntax of strings (see Strings).

If you call expand-last-match after a failed match, the returned value is not predictable.

Function: string-replace regexp template string

Returns the string created by replacing all matches of regexp in string with the result of expanding template using the expand-last-match function.

(string-replace "-" "_" "foo-bar-baz")
    ⇒ "foo_bar_baz"

(string-replace "&(optional|rest)" "#!\\1" "(a &optional b &rest c)")
    ⇒ "(a #!optional b #!rest c)"
Function: string-split regexp string

Return a list of substrings of string, each delimited by regexp.

(string-split ":" "paths/are:often/seperated:by/colon")
    ⇒ ("paths/are/" "often/seperated" "by/colon")

Previous: , Up: Regular Expressions   [Contents][Index]