HackerRank - Regex - Grouping and Capturing

© HackerRank

Matching Word Boundaries

\b assert position at a word boundary.

Three different positions qualify for word boundaries :

  • Before the first character in the string, if the first character is a word character.
  • Between two characters in the string, where one is a word character and the other is not a word character.
  • After the last character in the string, if the last character is a word character.

Task

Found any match?

1
\b[aeiouAEIOU][a-zA-Z]*\b

Capturing & Non-Capturing Groups

()

Parenthesis () around a regular expression can group that part of regex together. This allows us to apply different quantifiers to that group. This allows us to apply different quantifiers to that group.

These parenthesis also create a numbered capturing. It stores the part of string matched by the part of regex inside parentheses.

These numbered capturing can be used for backreferences.

(?:)

(?:) can be used to create a non-capturing group. It is useful if we do not need the group to capture its match.

For Example

12 34 567 9

1
\s{1}(?=\d+)

Task

okokok! cya

1
(?:ok){3,}

Alternative Matching

Alternations, denoted by the | character, match a single item out of several possible items separated by the vertical bar. When used inside a character class, it will match characters; when used inside a group, it will match entire expressions (i.e., everything to the left or everything to the right of the vertical bar). We must use parentheses to limit the use of alternations.

Task

Mr.DOSHI

1
^(Mr\.|Mrs\.|Ms\.|Dr\.|Er\.)[a-zA-Z]{1,}$

Resources

Regex - Subdomains - Grouping and Capturing