Topic: 问个正则表达式的问题

  Print this page

1.问个正则表达式的问题 Copy to clipboard
Posted by: hafocus
Posted on: 2006-11-06 11:03

src:

package regex;

import java.util.regex.*;

public class Regextest
{

public static void main(String args[])
{

String str = "((num like '%1122%'))";

String regEx = "\\({2}num (.*)\\){2}";

/* String str = "aabbaabc";

String regEx = "a*";
*/

Pattern p = Pattern.compile(regEx);

Matcher m = p.matcher(str);
if (m.find())
{

System.out.println("sum:" + m.groupCount());

for (int i = 0; i <= m.groupCount(); i++)
{
System.out.println(i + ": " +m.group(i));
}
}
else
System.out.println("no match");

}

}

怎么会有两个匹配呢?

sum:1
0: ((num like '%1122%'))
1: like '%1122%'

还有就是匹配表达式
\\({2}num (.*)\\){2} 和 ( \\({2})num (.*)\\){2}
有什么不一样的吗?

谢谢

2.Re:问个正则表达式的问题 [Re: hafocus] Copy to clipboard
Posted by: why
Posted on: 2006-11-07 19:02

\\({2}num (.*)\\){2} ==> ((num like '%1122%'))
(.*) ==> like '%1122%'

do your reading regarding () in regex first... (?:.*)?
Thanks.

3.Re:问个正则表达式的问题 [Re: hafocus] Copy to clipboard
Posted by: hafocus
Posted on: 2006-11-09 16:27

O !

谢谢,那么\\({2}num (.*)\\){2} 和 ( \\({2})num (.*)\\){2}
什么区别呢?

我如何完全的匹配 ((num 任意长度内容 ))
呢?


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923