lambypie
lambypie

Reputation: 481

Coldfusion: how to extract a substring using regex

I have a string that has some double quoted substring in it, the " character. In between the double quotes is the data i want.

How can i write a regex to extract "the first data i want" and "the second data i want" from this:

'some string with "the first data i want" and "the second data i want"'

I tried the following code.

<cfset mydata = 'some string with "the first data i want" and "the second data i want"'/>

<cfset arrData = ListToArray(mydata ,'"') />

Upvotes: 1

Views: 6049

Answers (1)

Jason Larke
Jason Larke

Reputation: 5609

Presumably you could do something trivial like this:

<cfset matches = REMatch('"([^"]*)"', mydata) />
<cfdump var="#matches#" label="Example REMatch" />

Unfortunately this will also include the double quotes in the Match, and ColdFusion's Regular Expression engine is quite old and shoddy, so it doesn't have support for Lookaheads/Lookbehinds.

The double quotes can be easily replaced, but if you really want to use lookaheads and look-behinds you can resort to using Java's own pattern library.

<cfset matches = [] />
<cfset pattern = CreateObject("java","java.util.regex.Pattern").Compile('(?<=")[^"]*(?=")') />
<cfset matcher = pattern.Matcher(mydata) />
<cfloop condition="matcher.Find()">
    <cfset ArrayAppend(matches, matcher.Group()) />
</cfloop>

<cfdump var="#matches#" label="Example of Java Regex" />

Upvotes: 5

Related Questions