Solved

ReReplace Last Occurance

Posted on 2003-10-29
7
611 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
Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

 
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

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.

Question has a verified solution.

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

When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

860 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