Why does the regex work not as expected?

前端 未结 4 577
余生分开走
余生分开走 2021-01-25 14:17

There is a string which can have one or several string ranges. These are correct strings:

\"\"
\"asd-asd\"
\"asd-asd;asd-asd\"
\"asd-asd;asd-asd;\"
\"asd-asd;asd         


        
4条回答
  •  忘掉有多难
    2021-01-25 15:01

    There's a slight add to the Answer of @Tim. This regex is not matching the "asd-asd;asd-asd;" if you're using .Net regex library. But if you add a ';' as option before string ends then it'll cover all the cases.

    ^([^;-]+-[^;-]+(;[^;-]+-[^;-]+)*);?$
    

    Now this will match all the valid strings provided except the Invalid - "asd0-asd1-asd2"

提交回复
热议问题