Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ReReplace Last Occurance

Posted on 2003-10-29
7
Medium Priority
?
617 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimum High-Definition Video Viewing and Control

The ATEN VM0404HA 4x4 4K HDMI Matrix Switch supports 4K resolutions of UHD (3840 x 2160) and DCI (4096 x 2160) with refresh rates of 30 Hz (4:4:4) and 60 Hz (4:2:0). It is ideal for applications where the routing of 4K digital signals is required.

 
LVL 4

Accepted Solution

by:
procept earned 1200 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

722 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