首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >正则表达式和GWT

正则表达式和GWT
EN

Stack Overflow用户
提问于 2009-07-21 23:14:03
回答 5查看 40K关注 0票数 84

我的问题是:在GWT中使用正则表达式有没有好的解决方案?

例如,我对String.split(正则表达式)的使用不满意。GWT将代码转换为JS,然后使用正则表达式作为JS正则表达式。但是我不能使用像Java Matcher或Java模式这样的东西。但我需要这些来进行分组匹配。

有没有任何可能性或库?

我尝试过Jakarta Regexp,但我遇到了其他问题,因为GWT不能模拟这个库使用的Java SDK的所有方法。

我希望能够在客户端使用类似这样的东西:

代码语言:javascript
复制
// Compile and use regular expression
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(inputStr);
boolean matchFound = matcher.find();

if (matchFound) {
    // Get all groups for this match
    for (int i=0; i<=matcher.groupCount(); i++) {
        String groupStr = matcher.group(i);
        System.out.println(groupStr);
    }
} 
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2011-10-24 16:35:40

使用RegExp的相同代码可以是:

代码语言:javascript
复制
// Compile and use regular expression
RegExp regExp = RegExp.compile(patternStr);
MatchResult matcher = regExp.exec(inputStr);
boolean matchFound = matcher != null; // equivalent to regExp.test(inputStr); 

if (matchFound) {
    // Get all groups for this match
    for (int i = 0; i < matcher.getGroupCount(); i++) {
        String groupStr = matcher.getGroup(i);
        System.out.println(groupStr);
    }
}
票数 100
EN

Stack Overflow用户

发布于 2010-10-30 04:23:08

GWT2.1现在有一个RegExp类,可以解决您的问题:

票数 32
EN

Stack Overflow用户

发布于 2011-11-28 20:41:11

这个答案涵盖了所有模式匹配,而不是像这里的其他答案那样只有一个模式匹配:

功能:

代码语言:javascript
复制
private ArrayList<String> getMatches(String input, String pattern) {
    ArrayList<String> matches = new ArrayList<String>();
    RegExp regExp = RegExp.compile(pattern, "g");
    for (MatchResult matcher = regExp.exec(input); matcher != null; matcher = regExp.exec(input)) {
       matches.add(matcher.getGroup(0));
    }
    return matches;
}

...and示例用法:

代码语言:javascript
复制
ArrayList<String> matches = getMatches(someInputStr, "\\$\\{[A-Za-z_0-9]+\\}");
for (int i = 0; i < matches.size(); i++) {
    String match = matches.get(i);

}
票数 21
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1162240

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档