6 The regexp Library

The regexp library augments the text pattern in betaenv with four new attributes:

regexp_match
regexp_search
regexp_replace
regexp_replace_literally

All four operations give facilities for working with regular expressions in text strings.

6.1 Regular expression

A regular expression (regexp, for short) is a pattern that denotes a set of strings, possibly an infinite set. Searching for matches for a regexp is a very powerful operation that editors on Unix systems have traditionally offered.

Regular expressions are used to locate occurrences of substrings in text, where the substring is expected to be of a certain structure. E.g. the regexp '[Ww]ord' will match the substring 'word' or 'Word'. The four operations offer slightly different possibilities, described below:

6.2 regexp_match

Takes a regexp as enter parameter (in the form of a reference to a text, containing the regexp. Matches THIS(text) against the regexp. INNER is executed if THIS(text) matches the regexp, and the virtual notification noMatch is invoked otherwise. Returns true if a match is found, false otherwise. The regexp must be found starting at the current position of THIS(text).

6.3 regexp_search

Like regexp_match, except that the match is allowed to be found anywhere between the current position and the end of THIS(text).

6.4 regexp_replace

Like regexp_search, except that it takes a second enter parameter, replacement_string. Regexp_replace searches for the regexp, and replaces the matched substring of THIS(text) with the replacement string. The replacement string may contain \0, \1, ..., \9, representing the substring matched by the i'th parenthesis in the regexp. \0 represents the entire substring matched. INNER is executed after the replace has taken place.

6.5 regexp_replace_literally

Like regexp_replace, except that the replacement string is taken literally (i.e. \0, \1, etc. are not representing any matched substrings)

All four regexp operations defines the same local attributes:

Regexp_string are compiled implicitly by the operations to ensure efficient matching in all operations. If the same regexp_string is to be used several times, the repetition of this compilation can be avoided by generating an instance of the operation (say regexp_search), and then use that instance repeatedly (as is illustrated in the following example):

(# regexp_s: @mytext.regexp_search(# ... #);
do ...
regexp_string[]->regexp_s;(* first search, implicit regexp
compilation *)
...
regexp_s; (* repeating the search with the same regexp.
No regexp compilation *)
...
regexp_s; (* repeating the search with the same regexp.
No regexp compilation *)
...
#)

6.6 Syntax of Regular Expressions

The syntax of regular expressions in this library follows the syntax of Emacs regular expressions. Some of the documentation below is from the interactive Emacs Info system.

Regular expressions have a syntax in which a few characters are special constructs and the rest are ordinary. An ordinary character is a simple regular expression which matches that character and nothing else. The special characters are '$', '^', '.', '*', '+', '?', '[', ']' and '\'; no new special characters will be defined. Any other character appearing in a regular expression is ordinary, unless a '\' precedes it.

For example, 'f' is not a special character, so it is ordinary, and therefore 'f' is a regular expression that matches the string 'f' and no other string. (It does not match the string 'ff'.) Likewise, 'o' is a regular expression that matches only 'o'.

Any two regular expressions A and B can be concatenated. The result is a regular expression which matches a string if A matches some amount of the beginning of that string and B matches the rest of the string.

As a simple example, we can concatenate the regular expressions 'f' and 'o' to get the regular expression 'fo', which matches only the string 'fo'. Still trivial. To do something nontrivial, you need to use one of the special characters. Here is a list of them.

6.7 List of special characters

Note: for historical compatibility, special characters are treated as ordinary ones if they are in contexts where their special meanings make no sense. For example, '*foo' treats '*' as ordinary since there is no preceding expression on which the '*' can act. It is poor practice to depend on this behavior; better to quote the special character anyway, regardless of where is appears.

For the most part, '\' followed by any character matches only that character. However, there are several exceptions: characters which, when preceded by '\', are special constructs. Such characters are always ordinary when encountered on their own. Here is a table of '\' constructs.

6.8 Table of '\' constructs

6.9 A complicated regexp

Here is a complicated regexp, used to recognize the end of a sentence together with any whitespace that follows. It is given in BETA text string syntax to enable you to distinguish the spaces from the tab characters. In BETA text string syntax, the string constant begins and ends with a double-quote. '""' stands for a double-quote as part of the regexp, '\\' for a backslash as part of the regexp, '\t' for a tab and '\n' for a newline.

"[.?!][]""')]*\\($\\|\t\\| \\)[ \t\n]*"

This contains four parts in succession: a character set matching period, '?' or '!'; a character set matching close-brackets, quotes or parentheses, repeated any number of times; an alternative in backslash-parentheses that matches end-of-line, a tab or two spaces; and a character set matching whitespace characters, repeated any number of times.


Basic Libraries - Reference Manual
© 1990-2004 Mjølner Informatics
[Modified: Monday October 23rd 2000 at 11:49]