Anchors and positional assertions
Positional assertion does not match actual text. Their match is only based on position in the text.
Start/end of line/string
Meta-character | Matches |
---|---|
^ | at start of string and, depending on mode, also after any newline |
$ | at end of string (eventually before a newline) and, depending on mode, also before any newline |
\A | at start of string in any mode |
\Z | at end of string (eventually before a newline) in any mode |
\z | only at end of string |
Word boundaries
Meta-character | Matches |
---|---|
\b | word boundaries which are between /\W/ and /\w/ and between /\w/ and /\W/ |
\B | anywhere \b does not match |
\< | word starts which are between /\W/ and /\w/ (not available in most implementation, see lookarounds for an alternative) |
\> | word end which are between /\w/ and /\W/ (not available in most implementation, see lookarounds for an alternative) |
Lookarounds
Lookarounds permits to check a regular expressions against a text without actually matching it. Look-behind check that the current position is preceded by a text matching a given expression. To check a successful look-behind use (?<=…). The negative form is (?<!…). Some implementation allow only regular expression that match a fixed length string in a lookbehind.
Lookahead check that the current position is followed by a text matching a given expression. To check a successfull lookahead use (?=…). The negative form is (?!…).
Here is two samples that simulate word-start and word-end shorthands using lookaround:
- /(?<!\w)(?=\w)/ is equivalent to /\</
- /(?<=\w)(?!\w)/ is equivalent to /\>/
End of a previous match
\G could be used to start a match just after the previous one. It is usually used at the beginning of an expression. This is the only modifier that match a position depending on another match. The position is attached to the text and not to the regular expression which means that you can start the match of a different regex than the one that has previously match.