Extracting a file name from a string variable

lonnyo used Ask the Experts™
I have a soap call that is returning data in the following format:


I need to extra the directory and file name from the following section:

The id number can be any number of characters, so starting x number of characters in is not an option.

What is the best method for extracting the direcotry/filename.

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
this regex will do it:


Open in new window

group 1 holds the directory
group 2 holds the filename

(assuming coldfusion can do regular expressions...)
<cfset theString = "ID={43054}|Path={C134/e1e4c0cf-6927-4414-835d-aa1e84600a9d.JPG} ">

<cfset fullpath = listLast(theString,"{}")>


<cfset v = "ID={43054}|Path={C134/e1e4c0cf-6927-4414-835d-aa1e84600a9d.JPG} ">

<cfset x = listlast(v, "{")>
<cfset x = replace(x, "}", "")>
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

gd see we're on the same page but - listLast(theString,"{}")> wouldn't work would it?

that would imply delimiters of {}

another way to do is to use the regex from basicinsticnt and use reMatchNocase

<cfset v = "ID={43054}|Path={C134/e1e4c0cf-6927-4414-835d-aa1e84600a9d.JPG}">

<cfset x = rematchnocase("Path=\{([^/]+)/([^\}]+)}",v)>
<cfloop array="#x#" index="i">

because reMatch returns an array you have to output it in a cfloop

but you'd still have to clean it up


so the listlast is probably less lines of code
> gd see we're on the same page but - listLast(theString,"{}")> wouldn't work would it?

Hey Sid -
Many list functions can take multiple delimiters, such as cfloop.  I believe this is one of them.   Since listLast doesn't return empty values, specifying both should get the string from between them..
Most Valuable Expert 2015

<cfset fullpath = listLast(theString,"{}")>

That's what I would've tried. For some reason it returns an empty string under CF9, but works if you use listGetAt or getToken() instead.

      <cfset fullpath  = getToken(theString, 4, "{}")>
Most Valuable Expert 2015
  <cfset v = "ID={43054}|Path={C134/e1e4c0cf-6927-4414-835d-aa1e84600a9d.JPG}(space here)">

Never mind the trailing space is the culprit ;-)  Remove it and it works as expected.
cool, thanks..

so just throw in a trim or add space as another delimiter

cfset theString = "ID={43054}|Path={C134/e1e4c0cf-6927-4414-835d-aa1e84600a9d.JPG} ">

<cfset fullpath = listLast(trim(theString),"{}")>


oops that trailing space was mine in my test code so my test worked the same as agx's ie: it didn't

I didn't know about the multiple delimiter thingy - nice..


Sorry, I don't think I explained it right.  In the value that I get:


I only want to use:


I tried playing with these options, but I can't get it right.

mine produces


as does gd's & agx's ??
well, gd's splits' it up if you want the whole thing just output



Sorry about that, SidFishes. I pasted the code again and it worked perfect.<br /><br />gdemaria - first code did cause a problem because of the trailing space.
> gdemaria - first code did cause a problem because of the trailing space.

... so trim it...

Note that Sid's code leaves the trailing space connected to your resulting string, so you have to trim it no matter what.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial