我的问题是:在GWT中使用正则表达式有没有好的解决方案?
例如,我对 String.split(regex) 的使用不满意。 GWT 将代码翻译为 JS,然后将正则表达式用作 JS 正则表达式。但我无法使用 Java Matcher 或 Java Pattern 之类的东西。但我需要这些来进行小组匹配。
有没有可能或图书馆?
我尝试了 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);
}
}
使用 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);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)