Skip to main content
Version: 1.0.1

Regular Expression

Definition​

Regular Expression is used to process advanced strings through its own expression, these expressions will have their own rules and you must follow those rules for your expression to work

Commonly used characters​

CharacterDescription
.Matches any character.
^regexMatches at the start point.
regex$Matches at the end point.
[abc]Can match any character in the [] pair. Can match a or b or c.
[abc][xy]Sets the definition, can match a or b or c followed by x or y.
[^abc]When the ^ appears as the first character in square brackets, it negates the remaining patterns. This can match any character except a or b or c.
()Specifies a group (subexpression) as if it were a single element in the pattern. For example ((a(b))c) will match b, ab, abc.
[a-d1-7]Range: matches a string between a – d and the numbers 1 through 7
X | ZFinds X or Z.
XZFinds X followed by Z.
\dAny number, short for [0-9]
\DA non-numeric character, short for [^0-9]
\sspace character, short for [ \t\n\x0b\r\f]
\Snon-space character, short for [^\s]
\wletter character, short for [a-zA-Z_0-9]
\Wnon-letter character, short for [^\w]
\S+Some non-space characters (one or more).
\bCharacters in a-z or A-Z or 0-9 or _, short for [a-zA-Z0-9].
*Occurring 0 or more times, short for {0,}. For example A*B matches B, AB, AAB
+Occurs 1 or more times, short for {1,}. For example A+B matches AB, AAB
?Occurs 0 or 1 times, ? is short for {0,1}. For example A?B will match B or AB.
{X}Occurs X times. For example A{2} matches exactly the letter A
{X, }Occurs X times or more. For example A{2} matches AA, AAA, AAAA …
{X,Y}Occurs between X and Y times. For example A{2,4} matches AA,AAA,AAAA
*?* means appears 0 or more times, adding ? after means finding the smallest match.
CharacterDescription

Logical characters in regular expressions​

RegexDescriptionPatternExample
|Or22|3333
( … )Group characters and capture themA(nt|pple)Ant, Apple
\1Contents of Group 1r(\w)g\1xregex
\2Contents of Group 2(\d\d)\+(\d\d)=\2\+\112+65=65+12
(?: … )Groups are not captured, you cannot use \1A(?:nt|pple)Ant, Apple

Commonly used regular expressions​