Regular Expressions 101

Community Patterns

Match substring of two matches in either order

0

Regular Expression
PCRE2 (PHP >=7.3)

/
(longword1)(longword2)|.*\K(?1).*(?2)|.*\K(?2).*(?1)
/
gm

Description

When you have a block of text and you want to find the shortest substring with one of two matches on either end, in either order.

Submitted by Chris Barth <chrisjbarth@hotmail.com> - a year ago