我有一些使用Oracle正則表達式的代碼,我想將它們移植到GWT。在GWT正則表達式中使用捕獲組
public static void main(String[] args)
{
String expression = "(abc)|(def)";
String source = "abcdef";
Pattern pattern = Pattern.compile(expression);
Matcher matcher = pattern.matcher(source);
while (matcher.find())
{
if (matcher.start(1) != -1)
{
// it's an "abc" match
}
else if (matcher.start(2) != -1)
{
// it's a "def" match
}
else
{
// error
continue;
}
int start = matcher.start();
int end = matcher.end();
String substring = source.substring(start, end);
System.out.println(substring);
}
}
我試着將它移植到GWT的正則表達式庫,但它會通過啓動(int)方法,這似乎並沒有在GWT正則表達式得到支持捕獲組。
有沒有辦法模擬這種行爲?
的API參考:
請看一下:http://google-web-toolkit.googlecode.com/svn/javadoc/latest/com/google/gwt/regexp/ shared/RegExp.html你錯過了什麼? – 2012-02-16 00:30:20