特定の部分文字列を含む文から各単語を抽出する関数を作成しようとしています。たとえば、「Porky Pork Chop」で「Po」を検索すると、Porky Pork が返されます。
regexpal で正規表現をテストしましたが、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");
}
}