正则表达式非贪婪(懒惰) [英] Regex Non-Greedy (Lazy)
本文介绍了正则表达式非贪婪(懒惰)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图非贪婪地解析TD标签。我从这样的事情开始:
I'm attempting to non-greedily parse out TD tags. I'm starting with something like this:
<TD>stuff<TD align="right">More stuff<TD align="right>Other stuff<TD>things<TD>more things
我正在使用以下作为我的正则表达式:
I'm using the below as my regex:
Regex.Split(tempS, @"\<TD[.\s]*?\>");
记录返回如下:
""
"stuff<TD align="right">More stuff<TD align="right>Other stuff"
"things"
"more things"
为什么不分割第一个完整的结果(以东西开头的那个?如何调整正则表达式以拆分TD参数的所有实例,有或没有参数?
Why is it not splitting that first full result (the one starting with "stuff")? How can I adjust the regex to split on all instances of the TD tag with or without parameters?
推荐答案
你想要的正则表达式是< TD [^>] *>
:
The regex you want is <TD[^>]*>
:
< # Match opening tag
TD # Followed by TD
[^>]* # Followed by anything not a > (zero or more)
> # Closing tag
注意:。
匹配任何内容(包括空格)所以 [。\s] *?
是多余的,因为 [。]
与文字匹配。
所以请使用。*?
。
Note: .
matches anything (including whitespace) so [.\s]*?
is redundant and wrong as [.]
matches a literal .
so use .*?
.
这篇关于正则表达式非贪婪(懒惰)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文