GWT中的RegExp和MatchResult仅返回第一个匹配项 [英] RegExp and MatchResult in GWT returns only first match

查看:156
本文介绍了GWT中的RegExp和MatchResult仅返回第一个匹配项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在GWT中使用RegExp和MatchResult。它只返回一个单词中的第一个匹配项。我需要所有的三个克,我,米。我尝试了全球化,多行和不区分大小写的gim。但它不起作用。请找到下面的代码。

预期的输出是,无论大小写如何,它都应该在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);
}
}
}
}
}


解决方案

如果您需要收集所有匹配项,请运行 exec ,直到找不到匹配项。 b

要替换多次出现的搜索字词,请使用包含捕获组的 模式使用 RegExp#replace() >(我无法使 $& 反向引用GWT中的整个匹配工作)。



更改代码如下所示:

  if(dtoValue!= null){

//显示所有匹配
RegExp regExp = RegExp.compile(searchTerm,gi);
MatchResult matcher = regExp.exec(dtoValue);
while(matcher!= null){
System.out.println(matcher.getGroup(0)); //打印匹配值(演示)
matcher = regExp.exec(dtoValue);
}

//将所有searchTerm事件包装为1和0
RegExp regExp1 = RegExp.compile((+ searchTerm +),gi);
newDtoData = regExp1.replace(dtoValue,1 $ 10);
System.out.println(newDtoData);
// => 1On0 C1on0diti1on0
}

请注意 m (多行修饰符)只会影响模式中的 ^ $ ,因此您不需要它。

I am trying to use RegExp and MatchResult in GWT. It returns only first occurrence in a word. I need to have all the three "g", "i", "m". I tried "gim" which is global, multiline and case insensitive. But it is not working. Please find the code below. Thanks in advance.

The expected output is, it should find 3 matches of "on" in "On Condition" irrespective of the case.

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);
            }
        }
    }
  }
}

解决方案

If you need to collect all matches, run exec until you get no match.

To replace multiple occurrences of the search term, use RegExp#replace() with a pattern wrapped with a capturing group (I could not make $& backreference to the whole match work in GWT).

Change the code as follows:

if(dtoValue != null){

    // Display all matches
    RegExp regExp = RegExp.compile(searchTerm, "gi");
    MatchResult matcher = regExp.exec(dtoValue);
    while (matcher != null) { 
        System.out.println(matcher.getGroup(0));  // print Match value (demo)
        matcher = regExp.exec(dtoValue); 
    }

    // Wrap all searchTerm occurrences with 1 and 0
    RegExp regExp1 = RegExp.compile("(" + searchTerm + ")", "gi");
    newDtoData = regExp1.replace(dtoValue, "1$10");
    System.out.println(newDtoData);
    // => 1On0 C1on0diti1on0
}

Note that m (multiline modifier) only affects ^ and $ in the pattern, thus, you do not need it here.

这篇关于GWT中的RegExp和MatchResult仅返回第一个匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆