views:

179

answers:

2

Hi

As the title says, 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.log(str.match(/e/gim));

gives

["e", "e", "e"]

http://www.w3schools.com/jsref/jsref%5Fmatch.asp

regards Jon

+7  A: 

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));
}
Macarse
A: 

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.

sepp2k