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.
↳ 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.
$ 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.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With