Regex to find a number in a string

前端 未结 1 1276
北荒
北荒 2021-02-12 07:32

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.

1条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-12 08:16

    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)
提交回复
热议问题