GWT - 2.1 RegEx类来解析自由文本 [英] GWT - 2.1 RegEx class to parse freetext

查看:92
本文介绍了GWT - 2.1 RegEx类来解析自由文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力处理 com.google.gwt.regexp.shared.RegExp 类,并且只想解析字符串中的电话号码并获得所有出现的一个数字,但似乎只能得到第一次出现..我知道在Java(它的工作原理)和GWT之间的正则表达式存在细微的差异。

 字符串freeText =Theo Powell< 5643321309>,Robert Roberts< 9653768972>,Betty Wilson< 6268281885>,Brandon Anderson< 703203115> ;; 
MatchResult matchResult = RegExp.compile([\ +]?[0-9。 - ] {8,}).exe(freeText);
int groupCount = matchResult.getGroupCount(); // result = 1
String s = matchResult.getGroup(0); //结果= 5643321309

预先致谢。

Ian ..

解决方案

您必须循环,再次应用模式,直到它不返回任何内容。为此,您首先必须使用全局标志:

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));

$ / code>

如果你认为这有点魔术或kludgy )我建议阅读有关JavaScript RegExp 对象的文档,因为GWT中的 RegExp 类是这是一个直接的映射: https://developer.mozilla.org/en / JavaScript / Reference / Global_Objects / RegExp / exec (JS中的示例代码非常类似于上面的代码)。


I'm struggling with the com.google.gwt.regexp.shared.RegExpclass and simply want to parse the phone numbers from a string and get ALL occurrences of a number but only seems to be able to get the 1st occurrences.. I know there is subtle difference in the regex between java (where it works) and 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

Thanks in advance.

Ian..

解决方案

You'll have to loop, applying the pattern again until it returns nothing. For that, you first have to use the "global" flag:

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));
}

If you think it's a bit "magic" or "kludgy" (which it kind of is), I'd suggest reading docs about the JavaScript RegExp object, as the RegExp class in GWT is a direct mapping of this: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp/exec (with sample code in JS very similar to the one above).

这篇关于GWT - 2.1 RegEx类来解析自由文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆