正则表达式非贪婪(懒惰)

我试图非贪婪地解析TD标签。 我从这样的事情开始:

stuffMore stuffOther stuffthingsmore things 

我正在使用以下作为我的正则表达式:

 Regex.Split(tempS, @"\"); 

记录返回如下:

 "" "stuffMore stuffOther stuff" "things" "more things" 

为什么不拆分第一个完整的结果(以“stuff”开头的那个)? 如何在有或没有参数的情况下调整正则表达式以拆分TD标签的所有实例?

你想要的正则表达式是]*>

 < # Match opening tag TD # Followed by TD [^>]* # Followed by anything not a > (zero or more) > # Closing tag 

注意: . 匹配任何东西(包括空格)所以[.\s]*? 由于[.]与文字匹配,因此是多余的和错误的. 所以使用.*?

对于非贪婪的比赛,请尝试此

来自https://regex101.com/

  • *量词 – 在零和无限次之间匹配,尽可能多次,根据需要回馈(贪婪)
  • *? 量词 – 在零和无限时间之间匹配, 尽可能少 ,根据需要扩展(懒惰)