我正在尝试编写一个函数,从包含某个子字符串的句子中提取每个单词,例如在“Porky Pork Chop”中查找“Po”将返回 Porky Pork。
我已经在正则表达式上测试了我的正则表达式,但 Java 代码似乎不起作用。我究竟做错了什么?
private static String foo()
{
String searchTerm = "Pizza";
String text = "Cheese Pizza";
String sPattern = "(?i)\b("+searchTerm+"(.+?)?)\b";
Pattern pattern = Pattern.compile ( sPattern );
Matcher matcher = pattern.matcher ( text );
if(matcher.find ())
{
String result = "-";
for(int i=0;i < matcher.groupCount ();i++)
{
result+= matcher.group ( i ) + " ";
}
return result.trim ();
}else
{
System.out.println("No Luck");
}
}