Solved

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

Posted on 2014-03-21
4
514 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 15

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 74

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now