Regex to match continuous pattern of integer then space

后端 未结 4 943
一生所求
一生所求 2021-01-22 01:35

I\'m asking the user for input through the Scanner in Java, and now I want to parse out their selections using a regular expression. In essence, I show them an enu

4条回答
  •  星月不相逢
    2021-01-22 02:32

    Pattern pattern = new Pattern(^([0-9]*\s+)*[0-9]*$)
    

    Explanation of the RegEx:

    • ^ : beginning of input
    • [0-9] : only digits
    • '*' : any number of digits
    • \s : a space
    • '+' : at least one space
    • '()*' : any number of this digit space combination
    • $: end of input

    This treats all of the following inputs as valid:

    • "1"
    • "123 22"
    • "123 23"
    • "123456 33 333 3333 "
    • "12321 44 452 23 "

    etc.

提交回复
热议问题