How to write a Regular Expression – syntax

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
[xyz] matches any one of x, y or z
[^xyz] matches any character except x, y or z
[a-z] matches any one character between a and z
[^a-z] matches any one character except the characters between a and z

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.