Following example demonstrates how to search a perticular word in a string with the help of matcher.start() method of regex.Matcher class.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String args[]) {
Pattern p = Pattern.compile("j(ava)");
String candidateString = "This is a java program.
This is another java program.";
Matcher matcher = p.matcher(candidateString);
int nextIndex = matcher.start(1);
System.out.println(candidateString);
System.out.println("The index for java is:"
+ nextIndex);
}
}
Result:
The above code sample will produce the following result.
This is a java program. This is another java program.
The index for java is: 11