Solved

How should I format a regex for a positive lookbehind in ColdFusion?

Posted on 2014-03-21
4
542 Views
Last Modified: 2014-03-21
I have a string that contains a path, like "this/is/a/path/to/the/root/subfolder". I'm attempting to get everything after "root" (in this case "/subfolder".)

According to the documentation at regular-expressions.info, (?<=s)t should match the first "t" in "streets". I used that to make this:

<cfset regex = "(?<=root).*">
#REFind(regex, myString)#

Open in new window


This gives me the error "Reason: Sequence (?<...) not recognized."

I've Googled up a storm and tried escaping characters, changing the parentheses... but the most common error that throws is "?+* follows nothing in expression" with the occasional "Malformed regular expression" "?<="".

I'm at a loss. Can you help? Thanks!
0
Comment
Question by:SiobhanElara
4 Comments
 
LVL 16

Expert Comment

by:Gurpreet Singh Randhawa
ID: 39945889
try using like this


<cfset filepath = "this/is/a/path/to/the/root/subfolder">

<cfset fileName = ListLast(filePath, "/")>
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 39945941
CF out of the box doesn't support lookbehind. You can, however, use the regex engine of the underlying Java, which does support lookbehind. I am not a CF developer, though, so I can only point you to other resources. This article describes the process:

http://cfmlblog.adamcameron.me/2013/03/regular-expressions-in-coldfusion-part.html
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 250 total points
ID: 39945988
I'm not great with regex's, so I'll leave it to the regex gurus :) but you could also use array/list functions. If you want to go that route, the code below would do it.  Unfortunately CF lacks an GetSubArray type function so you have to roll your own.

<cfset str = "this/is/a/path/to/the/root/subfolder">
<!--- find position of "root" element in list --->
<cfset pos = listFindNoCase(str, "root", "/")>
<cfset result = "" />

<!--- if there's something after the "root" element, extract it --->
<cfif pos and pos lt listLen(str, "/")>
	<!--- convert to array and extract portion after "root" --->
	<cfset data = listToArray(str, "/", true).subList(pos, listLen(str, "/"))>
	<!--- convert it back to a string --->
	<cfset result = "/"& arrayToList(data, "/")>
</cfif>

<cfoutput>#result#</cfoutput>

Open in new window

0
 

Author Closing Comment

by:SiobhanElara
ID: 39946173
Thank you kaufmed and _agx_! I didn't know that about CF and the lookbehind.

(myselfrandhawa, I might also have something like this/is/a/path/to/the/root/subfolder/and/another, so I can't use ListLast. But thank you for the suggestion!)
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help with REGEX with 2 different strings 14 60
ColdFusion 10 Error 2 49
coldfusion cfloop help 6 31
Regular Expression Calculator Tester 2 72
by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question