Sterling Archer
Sterling Archer

Reputation: 22425

Coldfusion Regex

I have a HUGE string list (I'll limit the example to one line) with the format:

"[First Name] [Last Name] <[email address]>"

I ran a regular expression on the string to delimit this into an array.

<cfset x = REMatch("<(.*?)>",list) />

This works fine, except that it also returns the angular brackets <> around the email address

x[1] = <[email address]>

Just for simplicity sake because the cfdocs are quite regex ambiguous, I wrote this loop to remove the first and last character of each index..

<cfloop from="1" to="#arrayLen(x)#" index="y">
    <cfset a = #RemoveChars(x[y], 1, 1)# />
    <cfset a = #left(a,len(a)-1)# />
    <cfset x[y] = a />
</cfloop>

This works fine, yay, I have my array now.

However, it's not what I wanted. How can I return the email address WITHOUT the angular brackets included in the first place?

(Please note I also tried REReplace and REFind only returns the index of the occurrance)

(Also note that there are no [] brackets in the string, that's just saying the value inside isn't what I posted here)

Upvotes: 4

Views: 278

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336448

Coldfusion implements a regex flavor (Apache ORO, see this answer for details) that doesn't support lookbehind assertions which would be useful in this case.

But we can arrive at an approximation:

<cfset x = REMatch("[^<>]+(?=>)",list) />

should work as long as all angle brackets occur in unnested, balanced pairs.

Explanation:

[^<>]+  # Match one or more characters except angle brackets
(?=>)   # Make sure the next character is a closing angle bracket

Upvotes: 4

Related Questions