std
std.array
std.atomic
std.bitarray
std.exception
std.intrinsic
std.memory
std.regexp
std.thread
std.traits
std.unicode
std.vararg
std.math.core
std.math.ieee
std.math.special
|
|
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.
In the following guide, pattern[] refers to a
regular expression.
The attributes[] refers to
a string controlling the interpretation
of the regular 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
|
- const char[] email;
- Regular expression to extract an email address
- const char[] url;
- Regular expression to extract a url
- class RegExpException: object.Exception;
- One of these gets thrown on compilation errors
- 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.
Params:
char[] string |
String to search. |
char[] pattern |
Regular expression pattern. |
char[] format |
Replacement string format. |
char[] attributes |
Regular expression attributes. |
Returns:
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.
Params:
char[] string |
String to search. |
char[] pattern |
Regular expression pattern. |
char[] delegate(RegExp) dg |
Delegate |
char[] attributes |
Regular expression attributes. |
Returns:
the resulting string.
- int find(char[] string, char[] pattern, char[] attributes = null);
- Search string[] for first match with pattern[] with attributes[].
Params:
char[] string |
String to search. |
char[] pattern |
Regular expression pattern. |
char[] attributes |
Regular expression attributes. |
Returns:
index into string[] of match if found, -1 if no match.
- int rfind(char[] string, char[] pattern, char[] attributes = null);
- Search string[] for last match with pattern[] with attributes[].
Params:
char[] string |
String to search. |
char[] pattern |
Regular expression pattern. |
char[] attributes |
Regular expression attributes. |
Returns:
index into string[] of match if found, -1 if no match.
- 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.
string = String to search.
pattern = Regular expression pattern.
attributes = Regular expression attributes.
Returns:
array of slices into string[]
- RegExp search(char[] string, char[] pattern, char[] attributes = null);
- Search string[] for first match with pattern[] with attributes[].
Params:
char[] string |
String to search. |
char[] pattern |
Regular expression pattern. |
char[] attributes |
Regular expression attributes. |
Returns:
corresponding RegExp if found, null if not.
Example:
import std.stdio;
import std.regexp;
void main()
{
if (m; std.regexp.search("abcdef", "c"))
{
writefln("%s[%s]%s", m.pre, m.match(0), m.post);
}
}
// Prints:
// ab[c]def
- class RegExp;
- RegExp is a 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.
- this(char[] pattern, char[] attributes = null);
- Construct a RegExp object. Compile pattern
with attributes into
an internal form for fast execution.
Params:
char[] pattern |
regular expression |
char[] attributes |
attributes |
Throws:
RegExpException if there are any compilation errors.
- static RegExp opCall(char[] pattern, char[] attributes = null);
- Generate instance of RegExp.
Params:
char[] pattern |
regular expression |
char[] attributes |
attributes |
Throws:
RegExpException if there are any compilation errors.
- RegExp search(char[] string);
int opApply(int delegate(inout RegExp) dg);
- Set up for start of foreach loop.
Returns:
search() returns instance of RegExp set up to search string[].
Example:
import std.stdio;
import std.regexp;
void main()
{
foreach(m; RegExp("ab").search("abcabcabab"))
{
writefln("%s[%s]%s", m.pre, m.match(0), m.post);
}
}
// Prints:
// [ab]cabcabab
// abc[ab]cabab
// abcabc[ab]ab
// abcabcab[ab]
- char[] match(uint n);
- Retrieve match n.
n==0 means the matched substring, n>0 means the
n'th parenthesized subexpression.
if n is larger than the number of parenthesized subexpressions,
null is returned.
- char[] pre();
- Return the slice of the input that precedes the matched substring.
- char[] post();
- Return the slice of the input that follows the matched substring.
- char[][] split(char[] string);
- Split string[] into an array of strings, using the regular
expression as the separator.
Returns:
array of slices into string[]
- int find(char[] string);
- Search string[] for match with regular expression.
Returns:
index of match if successful, -1 if not found
- char[][] match(char[] string);
- Search string[] for match.
Returns:
If global attribute, return same value as exec(string).
If not global attribute, return array of all matches.
- 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.
If global, replace all matches. Otherwise, replace first match.
Returns:
the new string
- char[][] exec(char[] string);
- Search string[] for match.
Returns:
array of slices into string[] representing matches
- char[][] exec();
- Pick up where last exec(string) or exec() left off,
searching string[] for next match.
Returns:
array of slices into string[] representing matches
- int test(char[] string);
- Search string[] for match.
Returns:
0 for no match, !=0 for match
- int test();
- Pick up where last test(string) or test() left off, and search again.
Returns:
0 for no match, !=0 for match
- int test(char[] string, int startindex);
- Test string[] starting at startindex against regular expression.
Returns:
0 for no match, !=0 for match
- 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.
The format string has the formatting characters:
Format | Replaced With |
$$ | $ |
$& | The matched substring. |
$` | The portion of string that precedes the matched substring. |
$' | The portion of string that follows the matched substring. |
$n | The nth capture, where n is a single digit 1-9
and $n is not followed by a decimal digit. |
$nn | The nnth capture, where nn is a two-digit decimal
number 01-99.
If nnth capture is undefined or more than the number
of parenthesized subexpressions, use the empty
string instead. |
Any other $ are left as is.
- 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.
|
|