c++regexboost

How to match strings having optional group and not containing specific group?


How to match the following cases with regex:

XYZABC
ABC

but not these:

XYZ*ABC
*ABC

in order to capture the ABC part, which can be anything, including a (second) *?

I've tried the following, but it does not work.

(?:XYZ)?(?!\*)(.+)

Solution

  • You may use this regex with a possessive quantifier which is supported in boost c++ regex engine:

    ^(?:XYZ)?+(?!\*)(.*)
    

    RegEx Demo

    PS: Used \n inside negated character class for demo purpose only.

    RegEx Details: