Link to home
Start Free TrialLog in
Avatar of Plucka
PluckaFlag for Australia

asked on

ReReplace Last Occurance

Can I use ReReplace to replace the last occurance of a pattern with something else.

ie: I want to replace the last occurance of a space with a <br>

The Quick Brown Fox

I want to become

The Quick Brown<br>Fox

which would also work for 1 occurance.

Hello There

becomes

Hello<br>There
Avatar of Plucka
Plucka
Flag of Australia image

ASKER

Sorry, should have mentioned how I currently do this.

#Reverse(Replace(Reverse(test)," ",Reverse("<br>")))#

Which works fine, just makes for some unreadable code, thought there might be a simple regular expression method.

PS: I also know how to do this through loops and finds, but I'm after a single statement like above but simpler.
i know u have specifically asked for replacenocase

but just have a look at this

<CFSET var = "The Quick Brown Fox">

<CFSET x = ListGetAt(var,listlen(var,' '),' ')>
<CFSET var = ListDeleteAt(var,listlen(var,' '),' ') & "<br>" & x>

<CFOUTPUT>#var#</CFOUTPUT>

Regards
Hart
Avatar of Plucka

ASKER

That's probably only marginally easier to read code than my

#Reverse(Replace(Reverse(test)," ",Reverse("<br>")))#

I sometimes show this line of code to potential employees and ask them to tell me what it's doing.

I think there's probably a simple regular expression for this, but i'm not that good at regular expressions.

PS: I specifically asked for REreplace not replacenocase.
PPS: Wouldn't it be nice if Replace had a Scope option of last.
ASKER CERTIFIED SOLUTION
Avatar of procept
procept

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of procept
procept

Hi again,

one addition:

instead of reReplaceNoCase() you can use reReplace and change the regex to "([a-zA-Z0-9[:punct:]]+)$", but, I think the case insensitive comparison is a bit faster...

Chris

Avatar of Plucka

ASKER

Thanks, that worked a treat, thought there would be a regex method.
You're welcome. :-)