lookarounds - stam66/regexPrimerForLivecode GitHub Wiki


Token Meaning
(?<= ) Positive lookbehind
(?<! ) Negative lookbehind
(?= ) Positive lookahead
(?! ) Negative lookahead

Regex implements a few different ways of identifying start- and end-anchors for strings. One way to do this is with non-capturing groups (?: ). If there is a known string literal that precedes or follows the string we’re attempting to match, lookarounds identify string literals immediate preceding or following the string to match.

Lookarounds can either be a lookbehind, which identifies a string literal preceding the intended match, or a lookahead which describes the string literal that immediate follows the intended match.

Lookarounds can either be positive, meaning the string literal used must be present, or negative, meaning the string literal used must not be present.
Lookbehinds are of the form (?< ) with the < followed by “=“ for positive and “!” for negative.
Lookaheads are of the form (? ) with the “?” Followed by “=“ for positive and “!” For negative lookahead.

Positive lookbehind: (?<= )
Example: If the price of a product is £500 but we want to match the integers only: ____ (?<=£)\d+ matches 500. This is regex for:

  • 1 or more (+)
  • digits (\d)
  • immediately preceeded by “£” (?<=£)

Negative lookbehind: (?<! ) Example: if we have a text ££500 and don’t want to match digits preceded by ££: (?<!££)\d+ will avoid matching these digits. This is regex for:

  • 1 or more +
  • digits \d
  • not immediately preceded by “££” (?<!££)

These can be combined. If we have a string ££500, £400 and only want to match the values with a single £: (?<!££)(?<=£)\d+. This matches “400”.

Positive lookahead:(?= ) Example:Time: 3pm. \d+(?=pm) matches “3” This translates to

  • 1 or more +
  • digits \d
  • that are immediately followed by “pm”

Negative lookahead(?! )
Example:7 July, 9am. \d+(?!am) matches “7”

  • 1 or more +
  • digits \d
  • NOT followed immediately by “am” (?!am)
⚠️ **GitHub.com Fallback** ⚠️