Search code examples
regexnotation

Notations in Regular Expressions


I am trying to learn Regular Expressions. I've looked at several tutorials but I did not find them clear or comprehensive. My questions is when is ^$ used and when is "\b ." used? I know what they mean but just are sure how.

Some examples:

1. \((\d+)\)\.\((\d+)\)\.\((\d+)\)
2. \b1?264[)- ]*\d{3}[- ]*\d{4}\b
3. ^[a-g]{4}$

Don't all Regular Expressions have to start with "^" and end with "$" ?


Solution

  • Before seeing when they are used, first you need to know what they mean:

    • ^ is a start of line anchor.
    • $ is an end-of-line of line anchor.
    • \b matches a word boundary. In other words, it matches between a word character \w and either a non-word character \W or the start or end of the string.

    For example:

    • To check if a string starts with a digit use ^\d.
    • To check if a string ends with a digit use \d$.
    • To check if a string contains the word foo use \bfoo\b. Omitting the word boundaries would cause it to match words that contain foo such as seafood.