我正在努力应对com.google.gwt.regexp.shared.RegExp
类,只是想从字符串中解析电话号码并获取某个数字的所有出现,但似乎只能获取第一个出现。我知道 java(它工作的地方)和 GWT 之间的正则表达式存在细微差别。
String freeText = "Theo Powell<5643321309>, Robert Roberts<9653768972>, Betty Wilson<6268281885>, Brandon Anderson<703203115>";
MatchResult matchResult = RegExp.compile("[\+]?[0-9." "-]{8,}").exec(freeText);
int groupCount = matchResult.getGroupCount(); // result = 1
String s = matchResult.getGroup(0); //result = 5643321309
提前致谢。
Ian..
您必须循环,再次应用该模式,直到它不返回任何内容。为此,您首先必须使用“global”标志:
ArrayList<String> matches = new ArrayList<String>();
RegExp pattern = RegExp.compile("[\+]?[0-9. -]{8,}", "g");
for (MatchResult result = pattern.exec(freeText); result != null; result = pattern.exec(freeText)) {
matches.add(result.getGroup(0));
}
如果你认为它有点“神奇”或“笨拙”(确实如此),我建议阅读有关 JavaScript 的文档RegExp
对象,作为RegExp
GWT 中的类是其直接映射:https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp/exec https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp/exec(JS 中的示例代码与上面的非常相似)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)