2006-04-20
一个正则表达式的问题.
关键字: java
[code:1]
String needToMatch = "<table><tr>fadsf</tr><tr>dafqewrdf</tr></table>";
Pattern p = Pattern.compile("\\Q<tr>\\E.*\\Q</tr>\\E");
Matcher matcher = p.matcher(needToMatch);
while(matcher.find()){
System.out.println("I found the text \"" + matcher.group() +
"\" starting at index " + matcher.start() +
" and ending at index " + matcher.end() + ".");
}[/code:1]
得到的是
I found the text "<tr>fadsf</tr><tr>dafqewrdf</tr>" starting at index 7 and ending at index 39.
其实我想解析成<tr>fadsf</tr> 和 <tr>dafqewrdf</tr>
请问这个正则表达式该怎么写呢? thanks
String needToMatch = "<table><tr>fadsf</tr><tr>dafqewrdf</tr></table>";
Pattern p = Pattern.compile("\\Q<tr>\\E.*\\Q</tr>\\E");
Matcher matcher = p.matcher(needToMatch);
while(matcher.find()){
System.out.println("I found the text \"" + matcher.group() +
"\" starting at index " + matcher.start() +
" and ending at index " + matcher.end() + ".");
}[/code:1]
得到的是
I found the text "<tr>fadsf</tr><tr>dafqewrdf</tr>" starting at index 7 and ending at index 39.
其实我想解析成<tr>fadsf</tr> 和 <tr>dafqewrdf</tr>
请问这个正则表达式该怎么写呢? thanks
评论
yfmine
2006-04-22
学到了\Q\E,多谢.
推荐一本书:<C#字符串和正则表达式参考手册>
一个工具:The Regulator http://regex.osherove.com/
推荐一本书:<C#字符串和正则表达式参考手册>
一个工具:The Regulator http://regex.osherove.com/
hongliang
2006-04-21
明白叻。。。
dengyin2000
2006-04-21
引用
你在用greedy mode,用lazy mode就解决问题啦
jakarta ORO 的使用方法: <tr>.*?</tr>
hoho,前两天刚看了
sams.teach.yourself.regular.expressions.in.10.minutes
jakarta ORO 的使用方法: <tr>.*?</tr>
hoho,前两天刚看了
sams.teach.yourself.regular.expressions.in.10.minutes
下了这本书, 这个星期回家看看. 10分钟能看完么? 我英语好烂呀
dengyin2000
2006-04-21
引用
dengyin2000 写道:
hongliang 写道:
dengyin2000 写道:
引用:
请教一下,那个\Q和\E有什么用?
避免context中有特殊意义的字符, * { () ....
\\Q<tr>\\E.*?\\Q</tr>\\E
没感觉有啥作用。。。能详细解释一下吗?
String s = "abc*def*ghj";
试试System.out.println(s.split("*"));
怎么能这么写呢?应该是System.out.println(s.split("\\*"));吧
dengyin2000
文章时间: 2006-4-21 周五, 下午1:26 标题:
hongliang 写道:
dengyin2000 写道:
引用:
请教一下,那个\Q和\E有什么用?
避免context中有特殊意义的字符, * { () ....
\\Q<tr>\\E.*?\\Q</tr>\\E
没感觉有啥作用。。。能详细解释一下吗?
String s = "abc*def*ghj";
试试System.out.println(s.split("*"));
hongliang 写道:
dengyin2000 写道:
引用:
请教一下,那个\Q和\E有什么用?
避免context中有特殊意义的字符, * { () ....
\\Q<tr>\\E.*?\\Q</tr>\\E
没感觉有啥作用。。。能详细解释一下吗?
String s = "abc*def*ghj";
试试System.out.println(s.split("*"));
怎么能这么写呢?应该是System.out.println(s.split("\\*"));吧
dengyin2000
文章时间: 2006-4-21 周五, 下午1:26 标题:
hongliang 写道:
dengyin2000 写道:
引用:
请教一下,那个\Q和\E有什么用?
避免context中有特殊意义的字符, * { () ....
\\Q<tr>\\E.*?\\Q</tr>\\E
没感觉有啥作用。。。能详细解释一下吗?
String s = "abc*def*ghj";
试试System.out.println(s.split("*"));
假如 s = "abc*.def*.ghj" 呢 你是不是要 s.split("\\*\\.")这样呢? 万一有更多的保留字符呢 ? 用\Q\E吧. s.split("\\Q*.\\E")
hongliang
2006-04-21
dengyin2000 写道
hongliang 写道
dengyin2000 写道:
引用:
请教一下,那个\Q和\E有什么用?
避免context中有特殊意义的字符, * { () ....
\\Q<tr>\\E.*?\\Q</tr>\\E
没感觉有啥作用。。。能详细解释一下吗?
String s = "abc*def*ghj";
试试System.out.println(s.split("*"));
怎么能这么写呢?应该是System.out.println(s.split("\\*"));吧
dengyin2000
2006-04-21
hongliang 写道
dengyin2000 写道:
引用:
请教一下,那个\Q和\E有什么用?
避免context中有特殊意义的字符, * { () ....
\\Q<tr>\\E.*?\\Q</tr>\\E
没感觉有啥作用。。。能详细解释一下吗?
String s = "abc*def*ghj";
试试System.out.println(s.split("*"));
hongliang
2006-04-21
dengyin2000 写道
引用
请教一下,那个\Q和\E有什么用?
避免context中有特殊意义的字符, * { () ....
\\Q<tr>\\E.*?\\Q</tr>\\E
没感觉有啥作用。。。能详细解释一下吗?
yatwql
2006-04-21
你在用greedy mode,用lazy mode就解决问题啦
jakarta ORO 的使用方法: <tr>.*?</tr>
hoho,前两天刚看了
sams.teach.yourself.regular.expressions.in.10.minutes
jakarta ORO 的使用方法: <tr>.*?</tr>
hoho,前两天刚看了
sams.teach.yourself.regular.expressions.in.10.minutes
dengyin2000
2006-04-21
引用
请教一下,那个\Q和\E有什么用?
避免context中有特殊意义的字符, * { () ....
hongliang
2006-04-20
推荐一个写正则的插件QuickRex:
http://prdownloads.sourceforge.net/easyeclipse/eclipseplugin-quickrex-2.0.0.tar.gz
请教一下,那个\Q和\E有什么用?
http://prdownloads.sourceforge.net/easyeclipse/eclipseplugin-quickrex-2.0.0.tar.gz
请教一下,那个\Q和\E有什么用?
dengyin2000
2006-04-20
引用
java代码:
Pattern p = Pattern.compile("\\Q<tr>\\E.*?\\Q</tr>\\E");
Pattern p = Pattern.compile("\\Q<tr>\\E.*?\\Q</tr>\\E");
谢谢, 看了sun网站上的java tutorial的正则表达式这张. 对这节不是很清楚
http://java.sun.com/docs/books/tutorial/extra/regex/quant.html
[/code]
Current REGEX is: .*foo // greedy quantifier
Current INPUT is: xfooxxxxxxfoo
I found the text "xfooxxxxxxfoo" starting at index 0 and ending at index 13.
Current REGEX is: .*?foo // reluctant quantifier
Current INPUT is: xfooxxxxxxfoo
I found the text "xfoo" starting at index 0 and ending at index 4.
I found the text "xxxxxxfoo" starting at index 4 and ending at index 13.
Current REGEX is: .*+foo // possessive quantifier
Current INPUT is: xfooxxxxxxfoo
No match found.
茅塞顿开!
scud
2006-04-20
[code:1]
Pattern p = Pattern.compile("\\Q<tr>\\E.*?\\Q</tr>\\E");
[/code:1]
其实还是jakarta 的oro模块好用,更符合perl的习惯
sun实现的正则表达式不伦不类的,当然还是能用的
Pattern p = Pattern.compile("\\Q<tr>\\E.*?\\Q</tr>\\E");
[/code:1]
其实还是jakarta 的oro模块好用,更符合perl的习惯
sun实现的正则表达式不伦不类的,当然还是能用的
scud
2006-04-20
又见贪婪
- 浏览: 220486 次
- 性别:

- 来自: 广州

- 详细资料
搜索本博客
我的相册
VB-seamless
共 13 张
共 13 张
最近加入圈子
最新评论
-
使用Terracotta和Tomcat建 ...
renavatior 写道"运行start.bat 9081 这样我们就启动了目 ...
-- by rainsf -
使用Terracotta和Tomcat建 ...
"运行start.bat 9081 这样我们就启动了目录9081中的tomcat ...
-- by renavatior -
广州3年多经验 5500的 ...
fucku 写道广州的软件厂家可比深圳多多了,不过比起北京上海来,还是少了很多, ...
-- by yongfan_420 -
广州3年多经验 5500的 ...
广州的软件厂家可比深圳多多了,不过比起北京上海来,还是少了很多,导致机会也没有这 ...
-- by fucku -
广州3年多经验 5500的 ...
想高工资就去厂家咯,老在集成商里面混能有多大个奔头
-- by fucku






评论排行榜