我试图非贪婪地解析TD标签。我从这样的事情开始:
<TD>stuff<TD align="right">More stuff<TD align="right>Other stuff<TD>things<TD>more things
我正在使用以下作为我的正则表达式:
Regex.Split(tempS, @"\<TD[.\s]*?\>");
记录返回如下:
""
"stuff<TD align="right">More stuff<TD align="right>Other stuff"
"things"
"more things"
为什么不拆分第一个完整的结果(以“stuff”开头的那个)?如何使用或不使用参数调整正则表达式以拆分TD标签的所有实例?
答案 0 :(得分:34)
对于非贪婪的匹配,请尝试此<TD.*?>
答案 1 :(得分:14)
你想要的正则表达式是<TD[^>]*>
:
< # Match opening tag
TD # Followed by TD
[^>]* # Followed by anything not a > (zero or more)
> # Closing tag
注意:.
匹配任何内容(包括空格),因此[.\s]*?
是多余的而且错误,因为[.]
与文字.
匹配,因此请使用.*?
。
答案 2 :(得分:2)
*
量词 - 零和无限次之间的匹配,尽可能多
尽可能多的时间,根据需要回馈(贪婪)*?
量词 - 在零和无限次之间匹配,尽可能少,根据需要进行扩展(懒惰)