Learn Regex in 30 minutes (Stop dreaming)

Do you think you can learn it in 30 minutes? Stop dreaming. It needs lots of practices. But anyway, let’s still look at Regex together.

  • Character

Exact matching. Case sensitive.

  • The dot (.)
The dot (.)  is metacharacter, representing any character. 
b.g          big, bug, ...
n..e         name, nate, ...
  • []
You can specify every character or use '-' to define the range.
[0-9]        0,1,2,3,4,5,6,7,8,9
[A-z]        A,B,C,...,x,y,z
In some systems, A's value is smaller than z but it is opposite
in other systems. Be careful about this.
[aeiou]      a,e,i,o,u
[1-5a-co]    1,2,3,4,5,a,b,c,o
t[^eo]d      tad, tid,...   Find characters are not.
  • multipliers
*            zero or more times
+            one or more times
?            zero or one time

{3}          occurs three times
{2,4}        occurs between two and four times
{2,}         occurs at least two times
Regex is greedy matching by default. To change to not greedy mode, 
place a question mark behind the multiplier.
E.g.    l.*?k
  • \
\.            remove the special meaning of the next character
\s            match any character which is considered whitespace 
(space, tab etc)
\S            match any character which is not whitespace
\d            match any character which is a digit ( 0 - 9 )
\D            match any character which is not a digit
\w            match any character which is a word character 
equivalent to [a-zA-Z_0-9] 
\W            match any character which is not a word character

\t            match a tab
\r            match a carriage return
\n            match a line feed (or newline)

\b            match the beginning or end of a word
\<            match the beginning of a word
\>            match the end of a word
  • ^ (caret)

An anchor which matches the beginning of the line.

  • $ (dollar)

An anchor which matches the end of the line.

Special Thanks

I synthesised this blog based on Ryan’s tutorial. I strongly recommend reading his tutorial, which explains the regex syntax in a very neat way.

Here is the link: https://ryanstutorials.net/regular-expressions-tutorial/regular-expressions-basics.php

Leave a comment