Regex with all optional parts but at least one required

前端 未结 4 585
我寻月下人不归
我寻月下人不归 2020-12-22 02:03

I need to write a regex that matches strings like \"abc\", \"ab\", \"ac\", \"bc\", \"a\", \"b\", \"c\". Order is important and it shouldn\'t match multiple appearances of th

4条回答
  •  时光取名叫无心
    2020-12-22 02:12

    It is pointless to try to pack all functionality of all problems you ever have into one single regexp. The best solution is to write the obvious regex, and add a check against zero length. You should only get extra clever with regexps when you absolutely have to - for instance if you have to interact with an unchangeable API that accepts only one regexp and nothing more.

提交回复
热议问题