Java REGEX to match an exact number of digits in a string

后端 未结 5 1803
天涯浪人
天涯浪人 2020-12-11 04:19

I tried to find the answer to my problem in the questions history but they just come back in more than one thousand and after scanning through a few tens of matching answers

5条回答
  •  隐瞒了意图╮
    2020-12-11 04:40

    The first occurrence of 6 digits in the string you posted is actually 987654. If you mean the first occurrence of 6 digits surrounded by characters that are not digits, then this should work:

    (?

    EDIT: This approach uses a negative lookbehind and a negative lookahead. It's slightly different than the word boundary approach in that it will match 123456 in the following strings

    123456asdf some text hello
    
    another string a123456 aaaaaaaa
    

    If the numbers will always be surrounded by spaces then the word boundary approach is probably better.

提交回复
热议问题