使用正则表达式获取最后 table

get last table using regex

我正在尝试使用 coldfusion table 获取最后一个 table 及其所有内容,

这是它的结构类型:

<div>
<table><table>
<table></table>
<table></table> -  i need this one and all its contents
<div>

我如何在 coldfusion 中使用正则表达式来做到这一点

这是我的尝试:

<cfset data = rereplace(data,"(?s)(.*?<table.*?>.*?<\/table>.*?)(<table.*?>.*?<\/table>)(.*)","","ALL") />

但这似乎确实有效

此模式将完成工作:<table>.*<\/table>(?=\n*<div>)

它基本上是搜索 table 结束标记 </table>,然后是可选的 \n 和开始 div 标记 <div>

演示:https://regex101.com/r/vL9mP1/2

使用此模式,带 s 选项

.*(<table>.*?<\/table>)  

Demo

.               # Any character except line break
*               # (zero or more)(greedy)
(               # Capturing Group (1)
  <table>       # "<table>"
  .             # Any character except line break
  *?            # (zero or more)(lazy)
  <             # "<"
  \/            # "/"
  table>            # "table>"
)               # End of Capturing Group (1)

如果我要用正则表达式来做这个,我会做这样的事情..

<cfset ArrTables = rematchnocase("<table.*?>.*?</table>",data) />
<cfset lastTable = ArrTables[ArrayLen(ArrTables)] />

但是,正如一再提到的那样,基于正则表达式解析外部文档就像在沙子上盖房子。一个小的变化常常会导致应用程序的失败。程序员经常需要从文档中抓取数据,因此他们创建了专为该任务设计的工具。喜欢 jSoup.