views:

148

answers:

2

Given an HTML string like:

<span class="findme" id="31313131313">The Goods</span>

What kind of REGEX in Coldfusion would return just (if it's even possible?): 31313131313

Thanks!

+1  A: 

It is not a good idea to parse html using regex in general. Use an html parser instead.

That said, the following regex will give you the id from the given string.

<span[^>]*id="(\d+)"

The first group of the match, $1, will contain 31313131313.

It assumes a numeric id. For alphanumeric ones, replace \d with [0-9a-zA-Z]. You can use \w if _ is fine too.

Amarghosh
@Amarghosh thxs, it will always be numeric. I just tried this but it returned the entire string, no change. Any ideas why?
AnApprentice
Here is the full line: <cfset uniqueID = rereplace(results[i], '<span[^>]*id="(\d+)"', '\3')>
AnApprentice
Change `\3` to `\1` We've just one capturing group.
Amarghosh
Just tried that:<cfset uniqueID = rereplace(results[i], '<span[^>]*id="(\d+)"', '\1')>Same reults, it is returning everything
AnApprentice
I don't know CF, but try this: `<cfset uniqueID = rereplace(results[i], '.*<span[^>]*id="(\d+)".*', '\1')>`
Amarghosh
+1  A: 

Try, <span[^>]+?id="([^"]+)".*

According to your comment in Amarghosh answer, that would be

<cfset uniqueID = rereplace(results[i],'<span[^>]+?id="([^"]+)".*',"\1")>
S.Mark
Thanks Mark,, just tried that, it's close... It's returning:581-1268367477845>WTC Captive was created with a $1 billion FEMA grant and provides insurance coverage</span>
AnApprentice
updated! ........
S.Mark
OK this did it! <cfset uniqueID = rereplace(results[i],'<span[^>]+?id="([^"]+)">(.*)',"\1")>
AnApprentice