Is it possible to specify a regex that matches x...(a and b)...y
within a limited length of n
?
To be more precise:
- The length of chars between the matched
x
andy
must be at mostn
. - Both an
a
and ab
(regardless of order) must exist between the matchedx
andy
. x
,a
,b
, andy
here could stand for a multi-char string snippet.
Test cases (assume n
= 10):
Match:
...x..a...b..y...
...x..b...a..y...
...x..a...b..y...a...
...x..a...b..y...b...
...x..a...b..y...y...
...x..a.x.b..y...
...x..a.y.b..y...
...xaabbaabbay...
...x..a...b..y... ...x..a...b..y... (2 matches)
...xaby...xaby... (2 matches)
Don't match:
...x..a...b......
...x......b..y...
...x..a......y...
...x..a...b......y...
...x......b..y...a...
...x..a......y...b...
...x..a.y.b......
...x..a.y.b......y...
...x..a.y.b.x....y...
.a.x......b..y...
.b.x..a......y...
P.S: I know that this can be done by simply match /x.{0,n}y/
and then check whether a
and b
both exist in the matched string in many programming languages. However, this question explicitly requests for a single regex approach, so that it can be used as a query in some applications, such as Google Doc and Notepad++.