Regex to match continuous pattern of integer then space

后端 未结 4 974
一生所求
一生所求 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条回答
  •  萌比男神i
    2021-01-22 02:09

    To "parse out" the integers, you don't necessarily want to match the input, but rather you want to split it on spaces (which uses regex):

    String[] nums = input.trim().split("\\s+");
    

    If you actually want int values:

    List selections = new ArrayList<>();
    for (String num : input.trim().split("\\s+"))
        selections.add(Integer.parseInt(num));
    

提交回复
热议问题