Create array of regex matches

后端 未结 6 1619
醉话见心
醉话见心 2020-11-22 16:21

In Java, I am trying to return all regex matches to an array but it seems that you can only check whether the pattern matches something or not (boolean).

How can I u

6条回答
  •  不要未来只要你来
    2020-11-22 16:33

    From the Official Regex Java Trails:

            Pattern pattern = 
            Pattern.compile(console.readLine("%nEnter your regex: "));
    
            Matcher matcher = 
            pattern.matcher(console.readLine("Enter input string to search: "));
    
            boolean found = false;
            while (matcher.find()) {
                console.format("I found the text \"%s\" starting at " +
                   "index %d and ending at index %d.%n",
                    matcher.group(), matcher.start(), matcher.end());
                found = true;
            }
    

    Use find and insert the resulting group at your array / List / whatever.

提交回复
热议问题