Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a regular expression for this in android?

Suppose I have a string like this:

string = "Manoj Kumar Kashyap";

Now I want to create a regular expression to match where Ka appears after space and also want to get index of matching characters.

I am using java language.

like image 271
Rahul Vyas Avatar asked Jul 15 '09 07:07

Rahul Vyas


People also ask

What is regex Android?

↳ java.util.regex.Pattern. A compiled representation of a regular expression. A regular expression, specified as a string, must first be compiled into an instance of this class. The resulting pattern can then be used to create a Matcher object that can match arbitrary character sequences against the regular expression.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.


1 Answers

You can use regular expressions just like in Java SE:

Pattern pattern = Pattern.compile(".* (Ka).*");
Matcher matcher = pattern.matcher("Manoj Kumar Kashyap");
if(matcher.matches())
{
    int idx = matcher.start(1);
}
like image 68
Josef Pfleger Avatar answered Sep 23 '22 03:09

Josef Pfleger