How to create regex for this in android?

Suppose I have a line like this:

string = "Manoj Kumar Kashyap"; 

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

I am using java language.

+7
java android regex
source share
3 answers

You can use regular expressions, as in Java SE:

 Pattern pattern = Pattern.compile(".* (Ka).*"); Matcher matcher = pattern.matcher("Manoj Kumar Kashyap"); if(matcher.matches()) { int idx = matcher.start(1); } 
+15
source share

You do not need a regular expression for this. I am not a Java expert, but according to Android docs :

public int indexOf (line string)
Searches in this row for the first index of the specified row. The search for the line starts from the beginning and moves to the end of this line.

Options
enter the string to search.

Returns
the index of the first character of the specified string in this string, -1 if the specified string is not a substring.

You will probably get something like:

 int index = somestring.indexOf(" Ka"); 
+4
source share

If you really need regular expressions, not just indexOf , you can do this like this:

 String[] split = "Manoj Kumar Kashyap".split("\\sKa"); if (split.length > 0) { // there was at least one match int startIndex = split[0].length() + 1; } 
0
source share

All Articles