Regular Expressions

From RAD Studio
Jump to: navigation, search

Go Up to IDE Reference and Utilities


Regular expressions are search strings where certain characters (symbols) have a special meaning. RAD Studio recognizes the following symbols in regular expressions:

Character Description
^ A circumflex at the start of the string matches the start of a line.
$ A dollar sign at the end of the expression matches the end of a line.
. A period matches a single instance of any character. For example, b.t matches bot, and bat, but not boat.
? A question mark after a character or a character group matches zero or one occurrences of that character or group. For example, bo?t matches both bt and bot.
* An asterisk after a character or a character group matches any number of occurrences of that character or group, including zero occurrences. For example, bo*t matches bt, bot, and boot.
+ A plus sign after a character or a character group matches any number of occurrences of that character or a character group, with at least one occurrence. For example, bo+t matches bot and boot, but not bt.
| A vertical bar matches each expression on each side of the vertical bar. For example, bar|car will match either bar or car.
[ ] Characters inside square brackets match any character that appears in the brackets, but no others. For example, [bot] matches b, o, or t.
[^] A circumflex at the start of a string inside square brackets means NOT. Hence, [^bot] matches any characters except b, o, or t.
[-] A hyphen inside square brackets signifies a range of characters.

For example, [b-o] matches any character from b through o.

{ } Braces group characters or expressions. Groups can be nested, with a maximum number of 10 groups in a single pattern. For the Replace operation, groups are referred to by a backslash and a number, according to the position in the "Text to find" expression, beginning with 0. For example, given the text to find and replacement strings, Find: {[0-9]}{[a-c]*}, Replace: NUM\1, the string 3abcabc is changed to NUMabcabc.
( ) Parenthesis are an alternative to braces ({ }), with the same behavior.
\ A backslash before a wildcard character tells the Code Editor to treat that character literally, not as a wildcard. For example, \^ matches ^ and does not look for the start of a line.


If you enable BRIEF regular expressions, you can use the BRIEF regular expression symbols in your regular expressions, instead of the symbols in the table above. To enable BRIEF regular expressions, select Tools > Options > Editor Options and check the "BRIEF regular expressions" option. The following BRIEF symbols can be used:

Character Description
^ A circumflex at the start of the string matches the start of a line.
< A less than at the start of the string matches the start of a line.
% A percent sign at the start of the string matches the start of a line.
$ A greater than at the end of the expression matches the end of a line.
> A greater than at the end of the expression matches the end of a line.
? A question mark matches any single character. See . (period).
@ An at sign after a character or a character group matches any number of occurrences of that character or group, including zero occurrences. For example, bo@t matches bt, bot, and boot.
* An asterisk after a character or a character group matches any number of occurrences of that character or a character group, with at least one occurrence. For example, bo*t matches bot and boot, but not bt.
+ A plus sign after a character or a character group matches any number of occurrences of that character or a character group, with at least one occurrence. For example, bo+t matches bot and boot, but not bt.
| A vertical bar matches each expression on each side of the vertical bar. For example, bar|car will match either bar or car.
[ ] Characters in brackets match any one character that appears in the brackets and no others. For example [bot] matches b, o, or t.
[^] A circumflex at the start of a string inside square brackets means NOT. Hence, [^bot] matches any characters except b, o, or t.
[~] A tilde at the start of a string inside square brackets means NOT. Hence, [~bot] matches any characters except b, o, or t.
[-] A hyphen within the brackets signifies a range of characters. For example, [b-o] matches any character from b through o.
{ } Braces group characters or expressions. Groups can be nested, with the maximum number of 10 groups in a single pattern.
\ A backslash before a wildcard character tells the IDE to treat that character literally, not as a wildcard. For example, \^ matches ^ and does not look for the start of a line.

See Also

Code Samples