Abhishekh Gupta
Abhishekh Gupta

Reputation: 6236

Regex prefix lookaround not working in coldfusion 10

I am using reMatch to get the matched substring from a list. But when I am using prefix lookaround then I am getting error.

Sequence (?<...) not recognized

Code:

<cfset local.path = "schedule.category.classes.name,schedule.category.classes.id">
<cfset local.regex = "(?<=schedule.category.classes.)[a-zA-Z0-9_]*?(?=,|$)">
<cfset local.output = reMatch(local.regex, local.path)>

What am I missing?

Upvotes: 5

Views: 616

Answers (2)

Adam Cameron
Adam Cameron

Reputation: 29870

You're missing the bit about reading the docs ;-) - Regular expression syntax - Using special characters - Look behinds & arounds are not supported in CFML's flavour of regex (which is long-dead Apache ORO).

However it's easy enough to use java's regex implementation instead, which does support look-behinds: java.util.regex.Pattern - Special constructs (named-capturing and non-capturing).

I have written two parts of a three part series on using Java regexes in CFML: "Regular expressions in CFML (part 9: Java support for regular expressions (1/3))". I must get back to doing part 3 at some point, but what you need is in the first coupla parts anyhow.

Ben Nadel also writes extensively on using Java regexes in CFML. Just do a quick google if you get stuck having looked @ my notes (but let me know where you get stuck if you do, so I can revisit my wording!).

Upvotes: 4

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626689

Look-behinds are not available in Coldfusion regex patterns. Instead of reMatch, you can use REReplace to remove everything around the string you need to obtain:

<cfset local.path = "schedule.category.classes.name,schedule.category.classes.id">
<cfset local.regex = "schedule\.category\.classes\.([a-zA-Z0-9_]+).*$">
<cfset local.output = REReplace(local.path,local.regex,"\1")>

Upvotes: 1

Related Questions