User Tools

Site Tools


regex

Regex

Regular expressions The following regular expression characters are supported:

^ Circumflex - Constrain to start of line
A circumflex as the first character of the string constrains matches to start of lines.

Examples:
^Win Match lines beginning with Win
^Grep Match lines beginning with Grep


$ Dollar - Constrain to end of line
A dollar as the last character of the string constrains matches to end of lines.

Examples:
then$ Match lines ending with then
^end$ Match lines consisting of the single word end

. Period - Match any character
A period anywhere in the string matches any single character. 

Examples:
H..p Matches Help, Hoop, Harp etc.
H.w Matches Huw, How, Haw etc.
^Win.ers Matches lines beginning with Winders, Winners etc.


* Asterisk - Match 0 or more
An expression followed by a asterisk matches zero or more occurrences of that expression.

Examples:
to* Matches t, to, too etc.
00* matches 0, 00, 000, 0000 etc.

Note that specifying a single character followed by a * will cause Windows Grep to fail.


+ Plus - Match 1 or more
An expression followed by a plus sign matches one or more occurrences of that expression.

Examples:
to+ Matches to, too etc.
10+ Matches 10, 100, 1000, 10000 etc.
/([0-9]+/) Matches (0), (12464), (12) etc.

Note that specifying a single character followed by a + will cause Windows Grep to fail.


? Question mark - Optionally match
An expression followed by a question mark optionally matches that expression. 

Examples:
to? Matches t and to
10? Matches 1 and 10
Note that specifying a single character followed by a ? will cause Windows Grep to fail.


() Brackets - Expression group
Brackets can be used to group characters together prior to using a * + or ?.

Examples:
Win(dows)? Matches Win and Windows
B(an)*a Matches Ba, Bana and Banana


[ ] Square brackets - Character group
A string enclosed in square brackets matches any character in that string, but no others. If the first character of the string is a circumflex the expression matches any character except the characters in the string. A range of characters may be specified by two characters separated by a -. These should be given in ASCII order (A-Z, a-z, 0-9 etc.)

Examples:
{[0-9]} Matches {0}, {4}, {5} etc.
/([0-9]+/) Matches (100), (342), (4), (23456) etc.
H[uo]w Matches Huw and How
Gre[^py] Matches Green, Great etc. but not Grep, Grey etc.
[z-a] Matches nothing
^[A-Z] Match lines beginning with an upper-case letter


\ Backslash - Quote next character
A backslash quotes any character. This allows a search for a character that is usually a regular expression specifier. 

Examples:
\$ Matches a dollar sign $
\+ Matches a +


abc,def
[^?]* matches abc
[^?]*$ matches def

To use a regex in grep
grep -e regexPattern someStringOrFile
regex.txt · Last modified: 2022/10/18 19:06 by 127.0.0.1