Example in pseudo code: find a string 'foobar' that is followed by two sets of optional characters, the first set of characters might contain a b or c, in that order. Only zero or one a, zero or one b, and zero or one c is allowed. The second set of characters is always preceded by an X (to delimit that this is the second set) and might contain a b or c, Only zero or one a, zero or one b, and zero or one c is allowed in the second set. When the second set is present, it is always prececeded by an 'X': foobarfirst set of 'a','b',or 'c' X second set of 'a','b',or 'c' input: foobar matches, first set is empty, no X, no second set. foobarabc matches, first set contains abc, no X, no second set. foobaraXa matches, first set contains a, X delimiter, second set contains a. foobarabcXabc matches, first set contains abc, X delimiter, second set contains abc. foobarabcX must not match, nothing follows 'X' foobarabca must not match, no 'X' precedes last 'a'. In perl, matching up to the 'X' is easy: m/foobar(a)?(b)?(c?)X?/ But matching the next bit is tough. I've tried forward look/backward look assertions but can't nail the regexp I want. Adding to the difficulty is that all options in the first second, and all options in the second set, must be 'grouped' so I can retrieve the values from the expression.
In reply to perl look ahead regular expression that is optional? by bigsipper
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |