Regular Expression - Boundary Matcher

Regexp

Regular Expression - Boundary Matcher

About

The boundary matcher meta

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
< Matches the start of a word
> Matches the end of a word

Example

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.

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.

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.

Documentation / Reference





Discover More
Regexp
Multilingual Regular Expression Syntax (Pattern)

Regular expression are Expression that defines a pattern in text. This is therefore a language that permits to define structure of a text. They are a mathematically-defined concept, invented by Stephen...
Regexp
Regexp - Word Characters

A word can be represented by the shorthand class (\w) and is specified as: any letter (ie the class [A-Za-z]) or any digit (ie the class [0-9]) or the underscore character (ie the [_]) It would...
Regexp
Regular Expression - Line (Beginning and End)

Page the definition of line boundary (EOL) in regular expression You can define the begining or the end of a line with boundary matcher boundary matcher Description ^ The beginning of a line...
Regexp
Regular Expression - Meta-(symbols|characters) - Operator

There are two different sets of meta-characters: those that are recognized anywhere in the pattern except within square brackets, and those that are recognized in square brackets. Operator...



Share this page:
Follow us:
Task Runner