Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables.
There are many RegEx engines for use and these engines have different syntax and compilation. Perl5 is the most popular syntax which runs on NFA engine. There are three main types of engines: NFA, POSIX and DFA. Please see the references section at the end of the article for deatiled information.
Regular expressions are hard to explain by words and looks frightening. But if you have the patience and courage to jump into, it is one of the most useful and funny languages you may ever learn. So, here are the most used special characters, with examples.
Special Characters Used in Regular Expressions
"()" character
Matches the pattern between the parenthesis or used to logically group patterns or characters together.
RegEx: (exchange)
Match:
exchange in experts
exchange
"." character
The "dot" matches a single character. Note that it does not match line breaks unless the engine is operating in single line mode.
RegEx: experts.
Match: experts, experts
1, experts
a, ...
"*" character
This returns a result with zero or more occurences of the character before this. For example:
This returns a result with or without the character before it.
RegEx: experts?
Match: expert, expert
s
RegEx: exper(ts)?
Match: exper, exper
ts
"-" character
This character defines a range of characters in a character class. It also specifies a hyphen if placed immediately after the opening "[". If you want either "-" or "]" itself to be a member of a class, put it at the start of the list (possibly after a "^"), or escape it with a backslash. "-" is also taken literally when it is at the end of the list, just before the closing "]". The following all specify the same class of three characters: [-az] , [az-] , and [a\-z] . All are different from [a-z] , which specifies a class containing twenty-six characters, even on EBCDIC-based character sets. Also, if you try to use the character classes \w , \W , \s, \S , \d , or \D as endpoints of a range, the "-" is understood literally.
RegEx: [1-9]
Match: All numbers between 1 and 9
RegEx: [a-z]
Match: All lowercase letters from a to z
RegEx: [A-Z]
Match: All uppercase characters from A to Z
"[]" character
This matches the array or any of the characters enclosed.
RegEx: expert[SAB]
Match: expert
S, expert
A, expert
B
This matches the start of a string. This character will behave differently depending upon whether the engine is operating in multi-line mode or not; it will also match line breaks in multi-line mode.
RegEx: ^(experts) or \A(experts)
Match:
experts in
expertsexchange
RegEx: ^. or \A.
Match:
E in
Experts
If used in brackets, it has a negative meaning that matches the characters other than enclosed ones. This only applies to "^" character.
RegEx: [^exp]
Match: r, t, s in
experts
RegEx: [^1-9]
Match: m, s, t, r in m
1st
3r
"$" or "\Z" character
This matches the end of the string. This character will behave differently depending upon whether the engine is operating in multi-line mode or not; it will also match line breaks in multi-line mode.
RegEx: (exchange)$ or (exchange)\Z
Match:
exchange in experts
exchange
RegEx: .$ or .\Z
Match:
s in expert
s
"{n}" character
This matches exactly
n number of characters before it.
RegEx: experts{5}
Match:expert
sssss This expression pattern will not match "expert
ss" or "expert
s"
"{n,}" character
Matches the character before it at least n times
RegEx: experts{2,}
Match: expert
ss or expert
ssssss This will not match "experts"
"{n,m}" character
In this pattern, the integer "n" MUST BE smaller than or equal to "m". This matches the character before it at least "n" times AND at most "m" times
The "backslash" is the escape character for any special characters after it.
RegEx: expert\^
Match: expert^
RegEx: [\^\(]
Match: ^ or (
"\d", "\w" and "\s" characters
Matches
digits,
word characters (letters, digits, underscores) and white
spaces (tabs, spaces, line breaks) relatively.
RegEx: \dm1st3r\d
Match: 5m1st3r2
"\D", "\W" and "\S" characters
The negated versions of the above.
RegEx: 1\D2\W3\S4
Match: 1g2%354
"\b" character
This matches a backspace character when used inside a character class.
RegEx: [\b]
Match: \
Matches at the position between a word character (anything matched by \w) and a non-word character (anything matched by [^\w] or \W) as well as at the start and/or end of the string if the first and/or last characters in the string are word characters.
RegEx: .\b
Match:
s in expert
s
"\B" character
Matches at the position between two word characters (the position between \w\w) as well as at the position between two non-word characters (\W\W).
RegEx: \B.\B
Match:
x in e
xp
"|" character
The OR character matches either characters on the left and right side.
RegEx: x|rts
Match: x or rts in e
xpe
rts
RegEx: (x|r)ts
Match: x or r in e
xpe
rts
It can also be used to combine expression patterns:
RegEx: ((0?[1-9])|(1[0-9])|(2[0-9
]))
Match: Numbers from 1 to 29
"\Q...\E" character
Matches the characters between "\Q" and "\E", suppressing the meaning of special characters.
RegEx: "\Q+*/\E
Match: +*/
Now let's put these into use to understand it better:
Building a Date Expression
Let's build a date expression that will catch the dates in a text in the format of
MM/DD/YY.
Before using RegEx, I personally recommend you to decide which engine to use and read the resources about that engine as all the engines' behaviour differs when interpreting the special characters. Please review the references below if you want to dive deeper into Regular Expressions.
Our community of experts have been thoroughly vetted for their expertise and industry experience. Experts with Gold status have received one of our highest-level Expert Awards, which recognize experts for their valuable contributions.
Would it be OK with you if I link to your article from mine? I will not be presenting any information you covered; I will merely be pointing newcomers your way :)
Our community of experts have been thoroughly vetted for their expertise and industry experience. Experts with Gold status have received one of our highest-level Expert Awards, which recognize experts for their valuable contributions.
Thanks a lot this guide is proving useful having come from google docs complete lack of help on the topic. I hadn't even heard of "Regular expression" which must be one of the biggest misnomers in programming!
Comments (6)
Author
Commented:Commented:
Would it be OK with you if I link to your article from mine? I will not be presenting any information you covered; I will merely be pointing newcomers your way :)
Author
Commented:Sorry I was away for some time and just read your post. If you're still interested, you can link to my article.
Commented:
It seems I'm now the one who has been away for some time! Thanks. I'm finishing up one now :)
Commented:
I've still a way to go...if anyone can help i've got a question open on the topic..
https://www.experts-exchange.com/questions/28531374/REGEXEXTRACT-to-extract-a-subsection-of-text.html
Thanks a lot
View More