Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need a regex for dollar amount validation 4 61
Need some Regex Help 3 44
Scrolling feed with Coldfusion 3 32
Bulk Reorder File Names 4 53
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
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…

932 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

8 Experts available now in Live!

Get 1:1 Help Now