How to extract the last line of a file list in an HTML page with REGEX

I would like to use the http binding with the REGEX transform to extract from the following HTML file
the last line (i.e. the most recent file)

<h1>Directory Index</h1>
<a href="ipwebcam_videos">..</a>
<a href="ipwebcam_videos%2Fmodet%2Fmodet_2019-08-18_10-09.mp4">modet_2019-08-18_10-09.mp4</a>
<a href="ipwebcam_videos%2Fmodet%2Fmodet_2019-08-18_10-22.mp4">modet_2019-08-18_10-22.mp4</a>
<a href="ipwebcam_videos%2Fmodet%2Fmodet_2019-08-18_10-23.mp4">modet_2019-08-18_10-23.mp4</a>
<a href="ipwebcam_videos%2Fmodet%2Fmodet_2019-08-18_10-27.mp4">modet_2019-08-18_10-27.mp4</a>
<a href="ipwebcam_videos%2Fmodet%2Fmodet_2019-08-18_10-35.mp4">modet_2019-08-18_10-35.mp4</a>
<a href="ipwebcam_videos%2Fmodet%2Fmodet_2019-08-18_11-13.mp4">modet_2019-08-18_11-13.mp4</a>

If I define the following item

String FileList "[%s]" {http="<[*?<a .*?>(.*?)</a>.*)]"}

I only obtain ‘…’ , which corresponds to the first match of the REGEX.
Is there a way to obtain the last line, i.e. in this case

Thank you for your attention,

There is supposed to be a way to get the last match using a negative look ahead but I don’t think that will work in OH because the REGEX expression needs to match the whole string in the first place. You will have to probably process this HTML in a Rule.

It’s unfortunately that it isn’t well formed HTML (i.e. XHTML). If those <br> elements were </br> elements you could use the XPATH transform.

You might be able to do some stuff through executeCommandLine to take advantage of some command line looks built into Linux. For example,

val mostRecent = executeCommandLine("echo " + HTMLItem.state.toString + " | tail -4 | head -1 | cut -d \" -f 2", 1000)

That line should echo the HTML page, extract the last 4 lines of the file, then extract the first line from the result of the tail, and then return the stuff between the quotes.

1 Like