Regular Expression for number and dash

后端 未结 7 605
醉话见心
醉话见心 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:51

    @godwin: since I can't add a comment to your post and no one else seems to have responded, let me address at least one issue. At the beginning you have

    [\d+]{2}\-
    

    \d+ will match one or more digits and you're asking for it to be repeated twice, so the whole segment will match 123456789-:

    • [\d+] = 12345678 (greedy matching)
    • [\d+] = 9
    • \- = -

    You also probably didn't need to escape the dash because it's not inside a group block "[]".

提交回复
热议问题