www.digitalmars.com [Home] [Search] [D]
Last update Feb 22, 2005

Phobos: std.regexp

Regular expressions are a powerful method of string pattern matching. The regular expression language used is the same as that commonly used, however, some of the very advanced forms may behave slightly differently.

int find(char[] string, char[] pattern, char[] attributes = null)
Search string for first match with regular expression pattern with attributes. Returns index of match if found, -1 if no match.

int rfind(char[] string, char[] pattern, char[] attributes = null)
Search string for last match with regular expression pattern with attributes. Returns index of match if found, -1 if no match.

RegExp search(char[] string, char[] pattern, char[] attributes = null)
Search string for first match with regular expression pattern with attributes. Returns corresponding RegExp if found, null if not.

char[] sub(char[] string, char[] pattern, char[] format, char[] attributes = null)
Search string for matches with regular expression pattern with attributes. Replace each match with string generated from format. Return the resulting string.

char[] sub(char[] string, char[] pattern, char[] delegate(RegExp) dg, char[] attributes = null)
Search string for matches with regular expression pattern with attributes. Pass each match to delegate dg. Replace each match with the return value from dg. Return the resulting string.

char[][] split(char[] string, char[] pattern, char[] attributes = null)
Split string into an array of strings, using the regular expression pattern with attributes as the separator. Returns array of slices into string.

RegExp is a D class to handle regular expressions. It is the core foundation for adding powerful string pattern matching capabilities to programs like grep, text editors, awk, sed, etc. The RegExp class has these methods:
this(char[] pattern, char[] attributes)
Create a new RegExp object. Compile pattern with attributes into an internal form for fast execution. Throws a RegExpError if there are any compilation errors.

char[][] split(char[] string)
Split string into an array of strings, using the regular expression as the separator. Returns array of slices in string.

int find(char[] string)
Search string for match with regular expression.
Returns Description
>=0 index of match
-1 no match

char[][] match(char[] string)
Search string for match.
Attribute Returns
global same as call to exec(string)
not global array of all matches

char[][] exec(char[] string)
Search string for next match. Returns array of slices into string representing matches.

int test(char[] string)
Search string for next match.
Returns Description
0 no match
!=0 match

char[] replace(char[] string, char[] format)
Find regular expression matches in string. Replace those matches with a new string composed of format merged with the result of the matches.
Attribute Action
global replace all matches
not global replace first match
Returns the new string.

char[] replace(char[] format)
After a match is found with test(), this function will take the match results and, using the format string, generate and return a new string.

char[] replaceOld(char[] format)
Like replace(char[] format), but uses old style formatting:
Format Description
& replace with the match
\n replace with the nth parenthesized match, n is 1..9
\c replace with char c.

attributes

attributes are a string controlling the interpretation of the regulat expression. It consists of a sequence of one or more of the following characters:

Attribute Action
g global; repeat over the whole input string
i case insensitive
m treat as multiple lines separated by newlines

format

format strings are a sequence of characters with embedded format commands used to generate a new string from a regular expression match. The format commands are:

Format Description
$$ insert $
$& insert the matched substring
$` insert the string that precedes the match
$' insert the string that following the match
$n replace with the nth parenthesized match, n is 1..9
$nn replace with the nnth parenthesized match, nn is 01..99
$ insert $


Copyright (c) 2004-2005 by Digital Mars, All Rights Reserved