by Batuhan Cetin
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
Matches the pattern between the parenthesis or used to logically group patterns or characters together.
The "dot" matches a single character. Note that it does not match line breaks unless the engine is operating in single line mode.
Match: experts, experts1
This returns a result with zero or more occurences of the character before this. For example:
Match: expert, experts
Match: exper, experts
This returns a result with or without the character before it.
Match: expert, experts
Match: exper, experts
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.
Match: All numbers between 1 and 9
Match: All lowercase letters from a to z
Match: All uppercase characters from A to Z
This matches the array or any of the characters enclosed.
Match: expertA, expertB, expertC, ...., expertZ
Match: expert1, expert2, ...., expert9
"^" or "\A" character
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)
RegEx: ^. or \A.
If used in brackets, it has a negative meaning that matches the characters other than enclosed ones. This only applies to "^" character.
Match: r, t, s in expe
Match: m, s, t, r in m1
"$" 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
RegEx: .$ or .\Z
This matches exactly n
number of characters before it.
This expression pattern will not match "expertss
" or "experts
Matches the character before it at least n times
This will not match "experts"
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.
Match: ^ or (
"\d", "\w" and "\s" characters
ord characters (letters, digits, underscores) and whites
paces (tabs, spaces, line breaks) relatively.
"\D", "\W" and "\S" characters
The negated versions of the above.
This matches a backspace character when used inside a character class.
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.
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).
The OR character matches either characters on the left and right side.
Match: x or rts in ex
Match: x or r in ex
It can also be used to combine expression patterns:
Match: Numbers from 1 to 29
Matches the characters between "\Q" and "\E", suppressing the meaning of special characters.
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
We will start with the MM part:
Defining months written as: 1, 2, .., 9, 01, 02, .., 09
Defining months written as: 10, 11, 12
When we combine these with the "OR (|)
" operator, we get the month part:
Now let's define the DD part:
Defining days written as: 1, 2, .., 9, 01, 02, ..., 09
Defining days written as: 10, 11, ..., 29
Defining days written as: 30, 31
Combining these with the OR operator, we'll get:
Now the YYYY part:
Defines years between 1000 and 2999
Finally, let's combine them to get our MM/DD/YYYY result:
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.
The following is a great reference for learning more about how a regex engine (NFA) works:
Here is a detailed list of POSIX/NFS/DFA differences:
See you in another article