Java 正则表达式和 GWT
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1162240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regular Expressions and GWT
提问by ludwigm
My questions is: Is there a good solution to use regular expression in GWT?
我的问题是:在 GWT 中使用正则表达式有什么好的解决方案吗?
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.
例如,我对 String.split(regex) 的使用不满意。GWT 将代码转换为 JS,然后将正则表达式用作 JS 正则表达式。但是我不能使用 Java Matcher 或 Java Pattern 之类的东西。但我需要这些进行组匹配。
Is there any possibility or library?
有没有可能或图书馆?
I tried Jakarta Regexp, but I had other problems because GWT doesn't emulate all methods of the Java SDK this library uses.
我尝试了 Jakarta Regexp,但我遇到了其他问题,因为 GWT 没有模拟该库使用的 Java SDK 的所有方法。
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);
}
}
采纳答案by AleArnaiz
The same code using RegExp could be:
使用 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);
}
}
回答by Raze
If you want a pure GWT solution, I'm not sure it can be done. But if you're willing to use JSNI, you can use JavaScript's RegExp object to get the matched groups and all. You'll need to learn JSNI for GWT and JavaScript RegExp object.
如果你想要一个纯粹的 GWT 解决方案,我不确定它可以做到。但是如果你愿意使用 JSNI,你可以使用 JavaScript 的 RegExp 对象来获取匹配的组和所有内容。您需要为 GWT 和 JavaScript RegExp 对象学习 JSNI。
回答by Raze
The GWTx libraryseems to provide an emulation of java.util.regex.Pattern and friends. It doesn't look complete (Matcher in particular), but might be a good start.
该GWTx库似乎提供的java.util.regex.Pattern和朋友的仿真。它看起来并不完整(尤其是 Matcher),但可能是一个好的开始。
The technique they use to plug their own implementations of Java classes for the client side is the <super-source>
declaration in module XML. It's mentioned in GWT docs, module XML format description under "Overriding one package implementation with another". Standard JRE translatable classes in GWT are emulated the same way.
他们用来为客户端插入他们自己的 Java 类实现的技术是<super-source>
模块 XML 中的声明。它在 GWT 文档中提到,“用另一个包实现覆盖一个包实现”下的模块 XML 格式描述。GWT 中的标准 JRE 可翻译类以相同的方式进行模拟。
回答by Philippe Beaudoin
回答by Zbigniew Wiadro
This answer covers ALL pattern matches, not only one, as in other answers here:
这个答案涵盖了所有模式匹配,不仅仅是一个,就像这里的其他答案一样:
Function:
功能:
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 sample use:
...和样品使用:
ArrayList<String> matches = getMatches(someInputStr, "\$\{[A-Za-z_0-9]+\}");
for (int i = 0; i < matches.size(); i++) {
String match = matches.get(i);
}