- Home
- Learn Linux
- Learn Electronics
- Raspberry Pi
- Programming
- Projects
- LPI certification
- News & Reviews
This is a quick reference guide to some of the most common special characters used in regular expression matching. This is based on perl regular expressions, which are also used in php using preg_match and some other programming languages / applications.
\s White space [\t\n\r\f]
\S Not white space [^\t\n\r\f]
\d Digit [0-9]
\D Not digit [^0-9]
\w Word character [a-zA-Z_0-9]
\W Nonword character e.g. [^a-zA-Z_0-9]
* 0 or more
*? 0 or more, not greedy
+ 1 or more
+? 1 or more, not gready
? 0 or 1
?? 0 or 1, not gready
{x} Exactly x times
{x,y} Between x and y times
{x,y} Between x and y times, not gready
^ At the beginning of the string
$ At the end of the string
\ Escape chracter
\n New line
\r Carriage return
\t Tab
$n Bracketed group (n is which group) – non passive
$` Before matched string
$' After matched string
$+ Last matched string
$& Entire matched string
$_ Entire input string
$$ Literal $
. Any character except \n
(a|b) a or b
(...) Group
(?:...) Passive group
[abc] a, b or c
[^abc] not a or b or c
[a-c] Any letter from a to c
[A-C] Any upper case letter A to C
[0-5] Any digit 0 to 5
g Global match (multiple)
i Case-insensitive
m Multiple lines
s Treat string as single line
U Un-greedy pattern
^ [ . $ { * ( ) \ + | ? < >