Solved

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
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…
Suggested Courses

739 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