Regular Expressions 101

Community Patterns

Using negative lookahead

0

Regular Expression
PCRE2 (PHP >=7.3)

/
Chr\.(I(?!I)|III(?!I))
/
gm

Description

From the following:

Chr.
Chr.I
Chr.II
Chr.III
Chr.IIV
Chr.IIII.I

I only want to select Chr.I and Chr.III. One solution to this is using negative lookahead, which excludes certain follow-up characters once our string-of-interest is satisfied.

This is the regex: Chr\.(I(?!I)|III(?!I))

Submitted by dikip - 10 months ago