正则表达式和 GWT [英] Regular Expressions and GWT
问题描述
我的问题是:在 GWT 中使用正则表达式有什么好的解决方案吗?
My questions is: Is there a good solution to use regular expression in GWT?
例如,我对 String.split(regex) 的使用不满意.GWT 将代码转换为 JS,然后将正则表达式用作 JS 正则表达式.但是我不能使用 Java Matcher 或 Java Pattern 之类的东西.但我需要这些来进行组匹配.
I'm not satisfied with the use of String.split(regex) for example. GWT translates the Code to JS and then uses the regex as a JS regex. But I cannot use something like the Java Matcher or Java Pattern. But I would need these for group matching.
有没有可能或者图书馆?
Is there any possibility or library?
我尝试了 Jakarta Regexp,但我遇到了其他问题,因为 GWT 没有模拟该库使用的 Java SDK 的所有方法.
I tried Jakarta Regexp, but I had other problems because GWT doesn't emulate all methods of the Java SDK this library uses.
我希望能够在客户端使用这样的东西:
I want to be able to use something like this on the client side:
// 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 的相同代码可以是:
The same code using RegExp could be:
// 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);
}
}
这篇关于正则表达式和 GWT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!