DelphiFAQ Home Search:

Special characters in regular expressions

 

comments3 comments. Current rating: 3 stars (2 votes). Leave comments and/ or rate it.

. matches an arbitrary character, but not a newline unless it is a single-line match (see m//s).
(...) groups a series of pattern elements to a single element.
^ matches the beginning of the target. In multiline mode (see m//m) also matches after every newline character.
$ matches the end of the line. In multiline mode also matches before every newline character.
[ ... ] denotes a class of characters to match. [^ ... ] negates the class.
( ... | ... | ... ) matches one of the alternatives.
(?# TEXT ) Comment.
(?: REGEXP ) Like (REGEXP) but does not make back-references.
(?= REGEXP ) Zero width positive look-ahead assertion.
(?! REGEXP ) Zero width negative look-ahead assertion.
(? MODIFIER ) Embedded pattern-match modifier. MODIFIER can be one or more of i, m, s, or x.

Quantified subpatterns match as many times as possible. When followed with a ? they match the minimum number of times. These are the quantifiers:

+ matches the preceding pattern element one or more times.
? matches zero or one times.
* matches zero or more times.
{N,M} denotes the minimum N and maximum M match count. {N} means exactly N times; {N,} means at least N times.

A \ escapes any special meaning of the following character if non-alphanumeric, but it turns most alphanumeric characters into something special:

\w matches alphanumeric, including _, \W matches non-alphanumeric.
\s matches whitespace, \S matches non-whitespace.
\d matches numeric, \D matches non-numeric.
\A matches the beginning of the string, \Z matches the end.
\b matches word boundaries, \B matches non-boundaries.
\G matches where the previous m//g search left off.
\n, \r, \f, \t etc. have their usual meaning.
\w, \s and \d may be used within character classes, \b denotes backspace in this context.

Back-references:

\1 ... \9 refer to matched subexpressions, grouped with (), inside the match.
\10 and up can also be used if the pattern matches that many subexpressions.


Comments:

2009-02-18, 23:36:20
anonymous from India  
rating
Good Article
2011-11-22, 12:55:39
anonymous from Pakistan  
please any one tell the regular expression for special characters

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: