Day 44 - Explaning RegEx Syntax

In the last post about the Regular Expression I tried to explain what are regex with examples but doing that would make these post unnecssary large and might be hard to read so I will try to simply just explain all the keywords and sysntax of the regex with examples and all of it in this single post.

First of all about the basic concepts in the regex

Pipe |

The pipe character | works as OR between two patterns. Example: color|colour will search for either color or colour word.

Dot .

The dot . means match any character. Example: a.b will look for a followed by any character and then b, like azb or a0b etc.

Grouping

To group an expression parantheses ( ) are used. Example: The same expression color|colour can also be written as col(o|ou)r to look for color or colour.

Asterisk *

The asterisk * will look for zero or more occurences of the preceding character. Example: co*l will match words that have zero or more like cl, cool, coool or coooooool etc.

Plus Symbol +

The same as * but for one or more occurences of the preceding character. Example: co+l will match col, cool, cooooooool etc

Question Mark ?

The Question Mark ? will look for zero or only one occurence of the preceding character. Example: co?l will match col and cl only.

Curly Brackets{ }

Curly Brackets are used for matching an expression with given number of times. It syntax looks like this {min, max} where min is the minimum number and max is the maximum number of matches to look for.

  • {n} will match preceding element n times
  • {n, } will match preceding element n or more times
  • { ,n} will match preceding up to n times
  • {min, max} will match preceding element between min and max times

Caret Symbol ^

This character ^ mathces the starting position of a line.

Dollar Sign $

The Dollar Sign matches the end of the line.

Square Brackets [ ]

The Square brackets [ ] matches a single character that is inside the brackets. and if ^ Carte is added in the brackets like [^ ] it will match any character except the character inside the bracekts.

I think these are enough to write any pattern that you want with regex.


zainscizainsci