Regex to find a number in a string

前端 未结 2 1875
无人共我
无人共我 2021-02-12 07:18

I\'ve got a string that may or may not contain a number of 4 or 5 digits. I\'m looking for a regex that can detect if the string does in fact have such a number.

相关标签:
2条回答
  • 2021-02-12 07:51

    The foolproof one to avoid longer numbers would be:

    ([^\d]|^)\d{4,5}([^\d]|$)
    

    I'm assuming you don't want to allow for a comma after the thousands digit? If you do then:

    ([^\d]|^)\d{1,2},\d{3}([^\d]|$)
    
    0 讨论(0)
  • 2021-02-12 07:56

    Perhaps

    \d{4,5}
    

    ?

    0 讨论(0)
提交回复
热议问题