Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-03-21
4
Medium Priority
?
640 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1000 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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
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…

609 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