user3049861
user3049861

Reputation: 33

Regular expression match *abc* but not *bc*abc*bc*

I'm trying to find a regular expression to match all the combination of chars that have the string "question" but not have or contain the string "ion".

examples:

questionxxxx ------> match

xxxquestion--------> match

questionxxxxion----> not match

xxxquestionxxx-----> match

xxxionxxxquestion--> not match

I'm almost there but is missing me something!!!

this is what I have done:

((?=.*question(?!.*ion.*).).*)|^question$

This expression exclude all the strings with "ion" separated from "quest" but unfortunately also exclude "xxxquestion"

Upvotes: 3

Views: 1124

Answers (2)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

With perl compatible regex you can use this:

^(?:(question)|[^i]+|i(?!on))+(?(1)|(?!))$

With other regex flavors you can use this:

^(?:[^qi]+|q(?!uestion)|i(?!on))*question(?:[^i]+|i(?!on))*$

Upvotes: 0

OGHaza
OGHaza

Reputation: 4795

I have no idea how general you needed this solution to be, but the following works for your example:

(?!.*(?<!quest)ion)^.*question.*$

RegExr Example

Upvotes: 5

Related Questions