1

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!

AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012
  • You know regex shouldn't parse HTML right? http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags , and downvoted few people's answers, Do you still expecting something more? – YOU Mar 12 '10 at 06:08
  • Just trying to figure this out. – AnApprentice Mar 12 '10 at 06:10
  • Erik, thanks but this is different, the last question was to get the SPAN... This question is how to extract the ID. – AnApprentice Mar 12 '10 at 06:25
  • Any special reason why you ask the same question twice? http://stackoverflow.com/questions/2414576/coldfusion-regex-given-text-find-all-items-contained-in-spans – Tomalak Mar 12 '10 at 08:02

2 Answers2

2

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
  • 58,710
  • 11
  • 92
  • 121
2

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

According to your comment in Amarghosh answer, that would be

<cfset uniqueID = rereplace(results[i],'<span[^>]+?id="([^"]+)".*',"\1")>
YOU
  • 120,166
  • 34
  • 186
  • 219