Trying to build a regular expression to check pattern

后端 未结 7 741
我在风中等你
我在风中等你 2021-01-20 17:32

a) Start and end with a number
b) Hyphen should start and end with a number
c) Comma should start and end with a number
d) Range of number should be from 1-31

7条回答
  •  深忆病人
    2021-01-20 18:16

    Here is my workings

    Numbers:

    0|([1-9][0-9]*) call this expression A Note this expression treats zero as a special case and prevents numbers starting with a zero eg 0000001234

    Number or a range:

    A|(A-A) call this expression B (i.e (0|([1-9][0-9]*))|((0|([1-9][0-9]*))-(0|([1-9][0-9]*)))

    Comma operator

    B(,B)*

    Putting this togher should do the trick and we get

    ((0|([1-9][0-9]*))|((0|([1-9][0-9]*))-(0|([1-9][0-9]*))))(,((0|([1-9][0-9]*))|((0|([1-9][0-9]*))-(0|([1-9][0-9]*)))))*

    You can abbreviatge this with \d for [0-9]

提交回复
热议问题