[英]Regex Non-Greedy (Lazy)
I'm attempting to non-greedily parse out TD tags. 我正在尝试非贪婪地解析出TD标签。 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]*?\>");
The records return as below: 记录返回如下:
""
"stuff<TD align="right">More stuff<TD align="right>Other stuff"
"things"
"more things"
Why is it not splitting that first full result (the one starting with "stuff")? 为什么不拆分第一个完整结果(以“ stuff”开头的结果)? How can I adjust the regex to split on all instances of the TD tag with or without parameters? 如何调整正则表达式以在带有或不带有参数的TD标签的所有实例上进行拆分?
对于非贪婪匹配,请尝试此<TD.*?>
The regex you want is <TD[^>]*>
: 您想要的正则表达式是<TD[^>]*>
:
< # Match opening tag
TD # Followed by TD
[^>]* # Followed by anything not a > (zero or more)
> # Closing tag
Note: .
注: .
matches anything (including whitespace) so [.\\s]*?
匹配任何内容(包括空格),因此[.\\s]*?
is redundant and wrong as [.]
matches a literal .
是多余的,并且是错误的,因为[.]
与文字匹配.
so use .*?
所以用.*?
. 。
From https://regex101.com/ 从https://regex101.com/
*
Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy) *
量词-在零和无限制的时间之间进行匹配,并尽可能多地匹配,并根据需要返回(贪婪) *?
Quantifier — Matches between zero and unlimited times, as few times as possible , expanding as needed (lazy) 量词-匹配零到无限次, 次数尽可能少 ,根据需要扩展(延迟)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.