Solved

ReReplace Last Occurance

Posted on 2003-10-29
7
602 Views
Last Modified: 2013-12-24
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
0
Comment
Question by:Plucka
  • 3
  • 3
7 Comments
 
LVL 18

Author Comment

by:Plucka
ID: 9641188
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.
0
 
LVL 11

Expert Comment

by:hart
ID: 9641276
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
0
 
LVL 18

Author Comment

by:Plucka
ID: 9641345
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.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 4

Accepted Solution

by:
procept earned 300 total points
ID: 9641583
Hi,


<cfset newtext = reReplaceNoCase(myText, " ([a-z0-9[:punct:]]+)$", "<br>\1")>

will replace the last occurence of a space with a break, if the space is followd by letters, numbers, or a punctuation mark.

Explanation:
[[a-z0-9[:punct:]]+ means one or more letter, number or punctuation mark
$ means "at the end of the text"
<br>\1 is a backreference. Replace the reg ex with a break followed by the expression in parentheses.

If you need to allow other chars behond the space, let me know, should be possible,  too .

HTH,

Chris

0
 
LVL 4

Expert Comment

by:procept
ID: 9641593
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

0
 
LVL 18

Author Comment

by:Plucka
ID: 9641706
Thanks, that worked a treat, thought there would be a regex method.
0
 
LVL 4

Expert Comment

by:procept
ID: 9641752
You're welcome. :-)
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

A web service (http://en.wikipedia.org/wiki/Web_service) is a software related technology that facilitates machine-to-machine interaction over a network. This article helps beginners in creating and consuming a web service using the ColdFusion Ma…
Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now