Solved

ReReplace Last Occurance

Posted on 2003-10-29
7
609 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
website does not load without www 12 73
Forbidden errors 5 142
http to https 3 62
How to fix Redundant Hostnames notification from google analytic? 5 33
Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

773 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