Regular Expression - Boundary Matcher

> Procedural Languages > Multilingual Regular Expression Syntax (Pattern)

1 - About

The boundary matcher meta

Advertising

3 - List

Symbol Description
^ The beginning of a line
$ The end of a line
\b A word boundary
\B A non-word boundary
\A The beginning of the input
\G The end of the previous match
\Z The end of the input but for the final terminator, if any
\z The end of the input

4 - Example

4.1 - Word boundary

word boundary

Case 1:

  • The regex: \bdog\b
  • Input String to search: The dog plays in the yard
  • Result: Found the text dog starting at index 4 and ending at index 7.

Case 2:

  • The regex: \bdog\b
  • Input string to search: The doggie plays in the yard.
  • No match found.
Advertising

4.2 - Non-word boundary

A non-word boundary is \B.

Example 1:

  • The regex: \bdog\B
  • Input string to search: The dog plays in the yard.
  • No match found.

Example 2:

  • The regex: \bdog\B
  • Input string to search: The doggie plays in the yard.
  • I found the text dog starting at index 4 and ending at index 7.

4.3 - End of the previous match

To require the match to occur only at the end of the previous match, use \G:

Example 1:

  • The regex: dog
  • Input string to search: dog dog
  • Result:
    • I found the text “dog” starting at index 0 and ending at index 3.
    • I found the text “dog” starting at index 4 and ending at index 7.

Example 2:

  • The regexp: \Gdog
  • Input string to search: dog dog
  • Result: I found the text dog starting at index 0 and ending at index 3.

The second example finds only one match, because the second occurrence of dog does not start at the end of the previous match.

Advertising

5 - Documentation / Reference