Regex to capture an optional group in the middle of a block of input

后端 未结 3 2042
梦如初夏
梦如初夏 2021-01-16 08:03

I\'m stuck on a RegEx problem that\'s seemingly very simple and yet I can\'t get it working.

Suppose I have input like this:

Some text %interestingbi         


        
3条回答
  •  渐次进展
    2021-01-16 08:21

    Why do you have the extra set of parentheses?

    Try this:

    %interestingbit%.+?(?OPTIONAL_THING)?.+?%anotherinterestingbit%
    

    Or maybe this will work:

    %interestingbit%.+?(?OPTIONAL_THING|).+?%anotherinterestingbit%
    

    In this example, the group captures OPTIONAL_THING, or nothing.

提交回复
热议问题