我的问题是:在GWT中使用正则表达式有没有好的解决方案?
例如,我对String.split(正则表达式)的使用不满意。GWT将代码转换为JS,然后使用正则表达式作为JS正则表达式。但是我不能使用像Java Matcher或Java模式这样的东西。但我需要这些来进行分组匹配。
有没有任何可能性或库?
我尝试过Jakarta Regexp,但我遇到了其他问题,因为GWT不能模拟这个库使用的Java SDK的所有方法。
我希望能够在客户端使用类似这样的东西:
// 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);
}
}
发布于 2011-10-24 16:35:40
使用RegExp的相同代码可以是:
// 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);
}
}
发布于 2010-10-30 04:23:08
GWT2.1现在有一个RegExp
类,可以解决您的问题:
发布于 2011-11-28 20:41:11
这个答案涵盖了所有模式匹配,而不是像这里的其他答案那样只有一个模式匹配:
功能:
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示例用法:
ArrayList<String> matches = getMatches(someInputStr, "\\$\\{[A-Za-z_0-9]+\\}");
for (int i = 0; i < matches.size(); i++) {
String match = matches.get(i);
}
https://stackoverflow.com/questions/1162240
复制相似问题