Note: This article was originally published in 2009. Some steps, commands, or software versions may have changed. Check the current Programming documentation for the latest information.
Q: What are the special characters, escape sequences, and in general the syntax when doing regex (Regular Expressions)? A: . any character except a newline
d any decimal digit
D any non-digit
s any whitespace character
S any non-whitespace characte
w any alphanumeric character
W any non-alphanumeric character
number the contents of the group with the given number
- zero or more of the preceding block
*? zero or more of the preceding block (non-greedy)
+ one or more of the preceding block
+? one or more of the preceding block (non-greedy)
+ zero or one of the preceding block
+? zero or one of the preceding block (non-greedy)
{m} exactly m copies of the preceding block
{m, n} m to n copies of the preceding block
{m, n}? m to n copies of the preceding block (no-greedy)
^ beginning of line
$ end of line
b word boundary
B non-word boundary
(…) group
(?:…) non-grouping group
(?=…) matches if <…> matches next, but does not consume the string
(?!…) matches if <…> does NOT match next, but does not consume the string
x|y matches x or y
matches any one of x, y or z
matches any character except x, y or z
matches any one character between a and z
matches any one character except the characters between a and z
Summary
You’ve successfully learned write a regular expression – syntax. If you run into any issues, double-check the prerequisites and ensure your Programming environment is properly configured.