Regex to find a number in a string

前端 未结 2 2010
无人共我
无人共我 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]|$)
    

提交回复
热议问题