Skip to main content

Regex Patterns

Regular Expression, or regex in short, is a sequence of characters that specifies a search pattern in text.

This page shows a summary of regex constructs.

Characters

PatternDescriptionExampleSample Match
.Match any characterfile..5file_15
\dA digit: [0-9]file_\d\dfile_15
\DNot a digit:[^0-9]file_\D\Dfile_ab
\wA word character: [a-zA-Z_0-9]\w-\w\w\wA-b_1
\WNot a word charactera\W\Wa-+
\sA whitespace character.a\sba b
\SA non-whitespace charactera\Sba-b
\Escapes special charactera\.ba.b

Character classes Using []

PatternDescriptionExampleSample Match
[abc]a, b, or chell[AEIOU]hellO
[^abc]Any character except a, b, or c (negation)hell[^aeiou]hell_
[x-y]character in the range from x to y[A-Z]+GREAT
[^x-y]character not in the range from x to y[^A-Z]+great

Quantifiers

PatternDescriptionExampleSample Match
+One or more\d+12345
?zero or onehello!?hello
*Zero or moreAB*ABBB
{N}N timesx-\d{2}x-12
{N1,N2}N1 to N2 timesx-\d{2,4}x-123

Logical operators and groups

PatternDescriptionExampleSample Match
XYX followed by YHelloHello
X|YEither X or Ythis is (true|false)this is true (captures true, see bellow)
(X)X, as a capturing groupI ate (\w+) at lunchI ate carrots at lunch (captures carrots)
(?:X)X, as a non-capturing groupthis is (?:true|false)this is true (without capturing true)
\1Contents of Group 1r(\w)g\1xregex
\2Contents of Group 2(\d\d)+(\d\d)=\2+\112+65=65+12

Boundary matchers

PatternDescriptionExampleSample Match
^Start of line. (But when [^inside brackets], it means "not")^abc .*abc (line start)
$End of line.*? the end$this is the end
\ABeginning of string\Aabc[\d\D]*abc (string start)
\ZThe end of the inputthe end\Zthis is...\n...the end\n
\GThe end of the previous match
\bWord boundaryBob.*\bcat\bBob ate the cat
\BNot a word boundaryc.*\Bcat\B.*copycats
Note

Raw follows java conventions for regex patterns, see java documentation for more info.