How to match, but not capture, part of a regex?

前端 未结 7 1589
清酒与你
清酒与你 2020-11-28 00:10

I have a list of strings. Some of them are of the form 123-...456. The variable portion \"...\" may be:

  • the string \"apple\" followed by a hyphen,
7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-28 01:08

    Try:

    123-(?:(apple|banana|)-|)456
    

    That will match apple, banana, or a blank string, and following it there will be a 0 or 1 hyphens. I was wrong about not having a need for a capturing group. Silly me.

提交回复
热议问题