结果
上面的代码示例将产生以下结果。
This is a java program. This is another java program.
The index for java is: 11
以下是在字符串中搜索特定单词的示例。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String s1 = "sairamkrishna mammahe CAINIAOYA Pvt Ltd";
String regex = "\\bPoint\\b";
Pattern p1 = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher m1 = p1.matcher(s1);
while (m1.find()) {
System.out.print("Start index: " + m1.start());
System.out.print(" End index: " + m1.end() + " ");
System.out.println(m1.group());
}
}
}
上面的代码示例将产生以下结果。
Start index: 32 End index: 37 Point