Regular Expression for number and dash

后端 未结 7 624
醉话见心
醉话见心 2020-12-15 23:08

Currently I write the regex like this: /^([\\d+]*-)+([\\d]*-)+([\\d])*$/

I want the result follow this pattern 00-123-456-789 or 0012

7条回答
  •  情话喂你
    2020-12-15 23:29

    If I understand you correctly, you wish your regex to stand for strings which consist of a number group followed (optionally) by additional number groups with a - separator.

    \d+      # represents a number group
    (-\d+)*  # represents 0 or more additional number groups beginning with "-"
    

    So, together with the necessary beginning and end of line assertions, together we have:

    ^\d+(-\d+)*$
    

提交回复
热议问题