Skip to content Skip to sidebar Skip to footer

Java Equivalent Of Javascript's String.match()

What is the Java equivalent of JavaScript's String.match() I need to get an array or a list of all matches Example: var str = 'The quick brown fox jumps over the lazy dog'; console

Solution 1:

Check Regex tutorial

Your code should look something similar to this:

String input = "The quick brown fox jumps over the lazy dog";
Matcher matcher = Pattern.compile("e").matcher(input);

while ( matcher.find() ) {
    // Do something with the matched text
    System.out.println(matcher.group(0));
}

Solution 2:

Take a look at the Pattern and Matcher classes in the regex package. Specifically the Matcher.find method. That does not return an array, but you can use it in a loop to iterate through all matches.

Solution 3:

String.matches(String regex) is a more direct equivalent, but only use it for one-off regexes. If you'll be using it multiple times, stick with Pattern.compile as suggested.

Post a Comment for "Java Equivalent Of Javascript's String.match()"