Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find all matches to a regular expression in android

How can I find all matches to a regular expression in android. I founds it for Perl, Python etc but not for android.

like image 328
vaichidrewar Avatar asked Jul 30 '11 16:07

vaichidrewar


People also ask

How do you find the number of matches in a regular expression?

To count the number of regex matches, call the match() method on the string, passing it the regular expression as a parameter, e.g. (str. match(/[a-z]/g) || []). length . The match method returns an array of the regex matches or null if there are no matches found.

Does * match everything in regex?

Throw in an * (asterisk), and it will match everything. Read more. \s (whitespace metacharacter) will match any whitespace character (space; tab; line break; ...), and \S (opposite of \s ) will match anything that is not a whitespace character.

How do I find all words in a regular expression?

The regular expression \b[A]\w+ can be used to find all words in the text which start with A. The \b means to begin searching for matches at the beginning of words, the [A] means that these matches start with the letter A, and the \w+ means to match one or more word characters.

How do you check if a string matches a regex?

If you need to know if a string matches a regular expression RegExp , use RegExp.prototype.test() . If you only want the first match found, you might want to use RegExp.prototype.exec() instead.


1 Answers

Here's an example:

import java.util.regex.Matcher;
import java.util.regex.Pattern;
...
// Find all the words of "foo bar".
Matcher m = Pattern.compile("\\w+").matcher("foo bar");
while (m.find()) {
    System.out.println("Found: " + m.group(0));
}
like image 55
MRAB Avatar answered Oct 10 '22 15:10

MRAB