user2429578
user2429578

Reputation: 111

Getting string between two characters - Coldfusion

I'm struggling a bit with ColdFusion (not the language I ever write in).

I am trying to do a regex to get a part of a string. So for example, if my string is: D_CECILA23_CEC23423 I want the part that is between the 2 underscores.

This is the code I have so far, and it works for anything that is alpha characters, but when a number is thrown into the mix, it just breaks.

<cfset myStr = "D_CELCI_LISA">
<cfset myStr2 = reReplace(myStr, "([\w\d\%]+)(\_)([/ A-Z]+)(\_)([\w\d\?]+)", "\3", "all")     >

<cfoutput>
myStr: #myStr#<br />
myStr2: #myStr2#<br />
</cfoutput> 

Which gives me:

myStr: D_CELCI_LISA
myStr2: CELCI

Upvotes: 1

Views: 3194

Answers (2)

Birgit Pauli-Haack
Birgit Pauli-Haack

Reputation: 11

@user2429578 ListLast() and ListFirst() for the last or first element of a list.

Upvotes: 0

Adam Cameron
Adam Cameron

Reputation: 29870

If it really is as simple as getting the text between the first and second underscore character, you don't need a regex. This'll do it:

myStr2 = listGetAt(myStr, 2, "_");

That said, this should do for the regex in that context: ^.*_([^_]+)_.*$, eg:

myStr2 = reReplace(myStr, "^.*_([^_]+)_.*$", "\1", "all");

Upvotes: 5

Related Questions