Obviously regular expressions is a little, but powerful helper to implement various complex requirements. As it is a text search oriented functionality, it is extremely useful in DB based tasks related to data ETL layers, searching and other open text related logic. But it is used entirely everywhere. Almost every text editor support regex search file contents. Its very beneficial to know at least basic patterns by heart to quickly find relevant information in big text files. Probably most often developers have to analyse various log files or do bulk text replacements, where regex with all its benefits comes into scene.
Here I put together a few most often used Perl Compatible Regular Expressions for quick recall if you’re just not using it frequently and want quickly construct some straight forward queries. In my opinion it is a minimum set of keywords which allow to construct maximum flexible queries.
Element | Match Description | Sample |
---|---|---|
^ | Start of the string | “^\w*” ⇒ “Regex – simple, but need to remember patterns” |
$ | End of the string | “\w*$“ ⇒ “Regex – simple, but need to remember patterns“ |
\D | Any non digit character | “\D*“ ⇒ “Thomas Edisson born in 1847/02/11″ |
\ | Escape character | “(\\\.)“ ⇒ “Here is dot and backslash: \.“ |
\d | Any digit character | “(\d{4,4}/{1,1}\d{2,2}/\d{2,2})“ ⇒ “Thomas Edisson born in 1847/02/11“ |
. | Any character | “.*“ ⇒ “Regex – simple, but need to remember patterns“ |
\w | Any word character (0-9 and a-Z) | “\w*\W{3,3}\w*“ ⇒ “Regex – simple, but need to remember patterns” |
\W | Any non word character. Opposite to “\w” | “\W{3,3}“ ⇒ “Regex – simple, but need to remember patterns” |
[ ] | Any single character listed in brackets | “[psn]{1,2}$“ ⇒ “Regex – simple, but need to remember patterns“ |
[x-y] | Range of characters. | [0-5] results into (01234) or [a-e] results into (abcde) |
[^ ] | Any single character not listed in brackets | “^[^gx]+“ ⇒ “Regex – simple, but need to remember patterns” |
* | 0 or more match of preceding expression | “(\w*,*)“ ⇒ “Regex – simple, but need to remember patterns” |
+ | 1 or more match of preceding expression | “(\w*,+)“ ⇒ “Regex – simple, but need to remember patterns” |
| | Logical operator OR | “([a-z]+ )|([a-z]+,)“ ⇒ “Regex – simple, but need to remember patterns” |
( ) | Group of characters. Characters inside brackets are treated as continuous string. | “(, )“ ⇒ “Regex – simple, but need to remember patterns” |
{m,n} | Minimum of m and maximum of n of preceding expression | “e{2,2}“ ⇒ “Regex – simple, but need to remember patterns” |
\t | Tab character | “^\t\S+“ ⇒ “ Regex – simple, but need to remember patterns” (here “Regex” is indented by tab character) |
\s | White space character | “\s+- +“ ⇒”Regex – simple, but need to remember patterns” |
\S | Non white space character | “\S+“ ⇒ “Regex – simple, but need to remember patterns“ |
If you miss something here and want to write more advanced expressions, please check this link in GitHub, where you’ll find more of The PCRE engine (Perl Compatible Regular Expressions).
Recently, I was tormented by those regexes, because I had not much experience before.
I found a good helper, the Expresso tool, which really helps write and test any pattern.
Hi Marius,
thanks for sharing !
Best wishes!
Danas