A regular expression (regex) is a composable search pattern for matching sequences of characters in a string. You can use metacharacters to specify conditions for individual characters and sequences. This guide introduces some basic metacharacters.
Note: You can search online to learn more about regexes. This web tool uses regexes as implemented in JavaScript.
Metacharacter | Definition/Usage | Sample Regex | Matched Substring |
---|---|---|---|
Character sets | |||
\d | Any digit (0-9) | \d days | I rested for 3 days. |
\w | Any "word" character: letters (A-Z, a-z), digits (0-9), underscore (_) | \w\w | He has won. |
[ |
Specify a character set |
• [eo]mit • letter [A-Z] |
• emit, omit, transmit • the letter A, the letter a |
[^ |
Specify characters to exclude |
[^eo]mit |
emit omit transmit |
. | Any character except the newline character (\n) | Boeing.\w |
• Boeing A23 • Boeing-A23 • BoeingA23 |
Boundaries | |||
\b | Boundary between a \w character and a non-\w character (or start/end of string) | \b\w\w\b | She ate an apple. |
^ | Start of string | ^good | good good good |
$ | End of string | good$ | good good good |
Quantifiers | |||
0 or 1 | trees? | one tree, two trees | |
1 or more | [a-z]+ | I donated to UNICEF today. | |
0 or more | take.*away | takeaway, take the dog away | |
{ |
Specify a quantity or quantity range |
• [a-z]{3} • [a-z]{2,4} • [a-z]{2,} |
• a, an, the, this, these • a, an, the, this, these • a, an, the, this, these |
Other | |||
(?: |
Treat a sequence as a (non-capturing) group | (?:da){2} | data, tada, dada |
Separate 2 or more sequences as alternatives |
• one|two|three • (?:one|two) days? |
• one dog, two cats, three mice • Stay one day or two days. |
|
\ |
Escape a metacharacter so it is treated literally Note: The backslash itself also needs to be escaped to be treated literally. |
• 1.5 • 1\.5 • C:\\Documents |
• 1-5, 1.5, 1a5 • 1-5, 1.5, 1a5 • C:\Documents\test.txt |