Regular Expressions

Is a special text string for describing a search pattern. You can think of regular expressions as wildcards on steroids. You are probably familiar with wildcard notations such as *.txt to find all text files in a file manager.

Essentially allows us to find a pattern in a string.

Regexes are widely supported in many programming languages including Java, Python, C++, Perl, Javascript, and PHP. It is also supported in text processing programs advanced text editors, and some other programs.

Regular Expressions

Some commonly used commands with regular expressions are tr, sed, vi, and grep.

Regular Expressions

Here are SOME of the main rules:

      .           Any single character
      [chars]     Character class: Any character of the class ``chars''
      [^chars]    Character class: Not a character of the class ``chars''
      text1|text2 Alternative: text1 or text2

Regular Expressions

      ?           0 or 1 occurrences of the preceding text
      *           0 or N occurrences of the preceding text (N > 0)
      +           1 or N occurrences of the preceding text (N > 1)

Regular Expressions

      (text)      Grouping of text (used either to set the borders of an alternative as above, or to make backreferences, where the Nth group can be referred to on the RHS of a RewriteRule as $N)

Regular Expressions

      ^           Start-of-line anchor
      $           End-of-line anchor

Interval Regular Expressions

    {n}     matches the preceding char appearing 'n'times

Regular Expression examples

I will use grep to demonstrate, but there are other programs that could be used.

The basic syntax is grep regex file. Below I will just write the regex.

Extra practice

Check out the following Tutorial: