我想在GWT中使用RegExp和MatchResult。它只返回一个单词中的第一个匹配项。我需要所有的三个“克”,“我”,“米”。我尝试了全球化,多行和不区分大小写的“gim”。但它不起作用。请找到下面的代码。提前致谢。GWT中的RegExp和MatchResult只返回第一个匹配
预期的输出是,它应该在“On Condition”中找到3个匹配的“on”,与情况无关。
import com.google.gwt.regexp.shared.MatchResult;
import com.google.gwt.regexp.shared.RegExp;
public class PatternMatchingInGxt {
public static final String dtoValue = "On Condition";
public static final String searchTerm = "on";
public static void main(String args[]){
String newDtoData = null;
RegExp regExp = RegExp.compile(searchTerm, "mgi");
if(dtoValue != null){
MatchResult matcher = regExp.exec(dtoValue);
boolean matchFound = matcher != null;
if (matchFound) {
for (int i = 0; i < matcher.getGroupCount(); i++) {
String groupStr = matcher.getGroup(i);
newDtoData = matcher.getInput().replaceAll(groupStr, ""+i);
System.out.println(newDtoData);
}
}
}
}
}
尝试解决这种方式:'字符串newDtoData = DtoValue;' 和 'MatchResult的匹配= regExp.exec(dtoValue); (matcher!= null){newDtoData = newDtoData.replaceFirst(RegExp.quote(matcher.getGroup()),“”+ i); System.out.println(newDtoData); matcher = regExp.exec(dtoValue); }' –
这工作!谢谢@WiktorStribiżew – Kutty