Regular Expression (Regex) – basics

In this post, we will talk about the various symbols and what do they mean in the making of a regular expression. So, let’s see them one by one –

SubexpressionMatches
\^Start of a string/line
$ End of a string/line
\bWord boundary
\BNot a word boundary
\ABeginning of entire string
\zEnd of entire string
\ZEnd of entire string (except allowable final line
terminator)
.Any one character (except line terminator)
[…]Any one character from listed one
[\^…]Any one character from not the listed one

Please see this post to understand the working of above Symbols.

Let’s now move to Normal( greedy ), Reluctant (non-greedy), and Possessive (very greedy) quantifiers

Normal(greedy) Quantifiers

SubexpressionMatches
{m,n}Matches from m to n repetitions
{m,}Matches m or more repetitions
{m}Matches exactly m repetitions
{,n}Matches from 0 to n repetitions (Short for {0,n})
\*Matches 0 or more repetitions (Short for {0,})
+Matches 1 or more repetitions (Short for {1,})
?Matches exatcly 0 or 1 repetition (Short for {0,1})

Reluctant (non-greedy) Quantifiers

SubexpressionMatches
{m,n}?Reluctant quantifier for “from m to n repetitions”
{m,}?Reluctant quantifier for “m or more repetitions”
{m}?Reluctant quantifier for 0 up to n repetitions
\*?Reluctant quantifier: 0 or more
+?Reluctant quantifier: 1 or more
??Reluctant quantifier: 0 or 1 times

Possessive (very greedy) quantifiers

SubexpressionMatches
{m,n}+Possessive quantifier for “from m to n repetitions”
{m,}+Possessive quantifier for “m or more repetitions”
{m}+Possessive quantifier for 0 up to n repetitions
\*+Possessive quantifier: 0 or more
++Possessive quantifier: 1 or more
?+Possessive quantifier: 0 or 1 times

Now lets see various escapes and shorthands in regular expression

Escapes and Shorthands

SubexpressionMatches
\Escape (quote) character: turns most metacharacters
off; turns subsequent alphabetic into metacharacters
\QEscape (quote) all characters up to \E
\EEnds quoting begun with \Q
\tA Tab character
\rReturn (carriage return) character
\nANewline character
\fA Form feed
\w(small w)Character in a word (use w+ for a word)
\W(Capital w)A nonword character
\d(small d)A Numeric digit (use \d+ for an integer)
\D(capital d)A nondigit character
\s(small s)A Whitespace character
\S(capital s)A nonwhitespace character

Liked the article? Share this on

Leave a Comment

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