-1

I am trying to get the last table and all its contents using coldfusion,

Here is the kind of structure it has:

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

how can i do this with regex in coldfusion

here is my try something like this:

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

but this does seems to be working

bywalk
  • 25
  • 5
  • 1
    "not working" is vague. Be more specific. – Dan Bracuk Feb 06 '15 at 17:53
  • specific is: it does not get the table i want, it just skips and my data variable is empty – bywalk Feb 06 '15 at 17:55
  • This sounds very familiar... Did you ask this question before? – Leigh Feb 06 '15 at 18:11
  • nopes, i recently signed up this site, i used to ask questions on easycfm.com – bywalk Feb 06 '15 at 18:29
  • 1
    Check out jSoup - http://jsoup.org/. It is great for parsing HTML. – Scott Stroz Feb 06 '15 at 19:03
  • 1
    Your [code is identical to this thread](http://stackoverflow.com/a/27324259/104223). The answer is the same: use JSoup. It is designed specifically to parse HTML, regular expressions are not. – Leigh Feb 06 '15 at 19:15
  • i am using this now ]*>(?:[^<]+|<(?!/table>))+',lnk)> - to all tables in a array and use the last one, i tried arrayMax(session.tables_footer), but i am getting an error of non numeric value found – bywalk Feb 06 '15 at 19:39
  • 1
    Like I said, regular expressions are the *wrong* tool for this job. Not sure why you won't try JSoup. It is quite simple. Try it. – Leigh Feb 06 '15 at 20:47

3 Answers3

1

If I was going to do this with regex, I would do something like this..

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

But, as covered time and time again, parsing an external document based on regex is like building a house on sand. A small change can frequently be the downfall of an application. Programmers frequently need to scrape documents for data, so they create tools designed for the task. Like jSoup.

Regular Jo
  • 5,190
  • 3
  • 25
  • 47
0

use this pattern, w/ s option

.*(<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)
alpha bravo
  • 7,838
  • 1
  • 19
  • 23
-1

This pattern will do the work: <table>.*<\/table>(?=\n*<div>).

It is basically search for the table closing tag </table> which is followed by optionally \n and opening div tag <div>

Demo: https://regex101.com/r/vL9mP1/2

streetturtle
  • 5,472
  • 2
  • 25
  • 43
  • 1
    Only thing I can suggest: http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec0a38f-7ffb.html – streetturtle Feb 06 '15 at 18:36