Regular Expression for number and dash

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

    From what have you provided us, this regex ^-?(\d+-?)+$ matches this

    -00-123-456-789-
    -00123456789-
    00-123-456-789-
    00123456789
    

    but doesn't match this:

    00-123--457-789
    

    Breakdown:

    • the string can start with a dash (^-?)
    • it has some digits followed by an optional dash (\d+-?)
    • the last group can be repeated one or more times ((\d+-?)+)
    • the string ends ($)

提交回复
热议问题