Regex 101

Regex 101

Let's play around patterns

What is Regex?

A Regular Expression (RegEx) is a sequence of characters that defines a search pattern


Metachracters

Metacharacters are characters that are interpreted in RegEx engine.

[] ^ . $ * + ? {} () \ |

Square Brackets []

Square brackets specifies a set of characters you wish to match.

Caret ^

  • Caret is used to check if a string starts with a certain character.
  • At the starting of Square Bracket, inverts the sequence.

Period .

A period matches any single character in the given sequence.

Dollar $

The Dollar symbol is used to check if a string ends with a certain character.

Star *

The star symbol matches zero or more occurrences of the pattern left to it.

Plus +

The plus symbol matches one or more occurrences of the pattern left to it.

Question Mark ?

The question mark symbol matches zero or one occurrence of the pattern left to it.

Braces {}

{x,y}. This means at least x, and at most y repetitions of the pattern left to it

Alteration |

Basically the OR Operator

Group ()

Parentheses is used to group sub-patterns

Backslash \

Backlash is used to escape various characters including all meta-characters


Special Sequences

  • \A - Matches if the specified characters are at the start of a string.
  • \b - Matches if the specified characters are at the beginning or end of a word.
  • \B - Opposite of \b. Matches if the specified characters are not at the beginning or end of a word.
  • \d - Matches any decimal digit.
  • \D - Matches any non-decimal digit.
  • \s - Matches where a string contains any whitespace character.
  • \S - Matches where a string contains any non-whitespace character.
  • \w - Matches any alphanumeric character (digits and alphabets).
  • \W - Matches any non-alphanumeric character.
  • \Z - Matches if the specified characters are at the end of a string.

Did you find this article valuable?

Support Cloud Maestro by becoming a sponsor. Any amount is appreciated!