Pattern | Meaning | Example Match |
---|---|---|
. |
Any character except newline | a , 1 , @ |
\\w |
Word character ([a-zA-Z0-9_] ) |
a , 9 , _ |
\\W |
Non-word character ([^a-zA-Z0-9_] ) |
@ , space |
\\d |
Digit ([0-9] ) |
0 –9 |
\\D |
Non-digit ([^0-9] ) |
a , - |
\\s |
Whitespace (space, tab, newline) | ' ' , \\n |
\\S |
Non-whitespace | a , 1 , @ |
Pattern | Meaning | Example |
---|---|---|
* |
0 or more | lo* → l , lo , loo |
+ |
1 or more | lo+ → lo , loo , not l |
? |
0 or 1 | colou?r → color , colour |
{n} |
Exactly n | a{3} → aaa |
{n,} |
n or more | a{2,} → aa , aaa , aaaa |
{n,m} |
Between n and m | a{2,4} → aa , aaa , aaaa |
Pattern | Meaning | Example |
---|---|---|
^ |
Start of string | ^Hello matches "Hello world" |
$ |
End of string | world$ matches "Hello world" |
\\b |
Word boundary | \\bword\\b matches "word" but not "sword" |
\\B |
Not a word boundary | \\Bend matches "bend" , not "end" |
Pattern | Meaning | Example |
---|---|---|
[abc] |
Matches a , b , or c |
[ch]at matches cat , hat |
[^abc] |
Not a , b , or c |
[^a-z] excludes lowercase |
[a-z] |
Range from a to z |
[0-9] , [A-Z] |
[a-zA-Z0-9_] |
Custom set | All letters + numbers + underscore |
Pattern | Meaning | Example |
---|---|---|
(abc) |
Group abc together |
(ha)+ matches haha , hahaha |
` | ` | OR operator |
(?:...) |
Non-capturing group | (?:ab)+ matches abab without capturing |
Use \\
before a special character to match it literally:
Character | Match This |
---|---|
\\. |
a literal . |
\\* |
a literal * |
\\\\ |
a literal backslash |