My recent suggestion didn't work. Leave the ? in there!
<cfset oldname="ben"> <cfset newname="mike> <cfset newStr=rereplace(str,"(^|[^[a-zA-Z]]?)#oldName#([^[a-zA-Z]]?|$)","\1#newName#\2","ALL")> <!--- second one to handle ben ben as per ben ben doom ---> <cfset oldname="ben"> <cfset newname="mike> <cfset newStr=rereplace(str,"(^|[^[a-zA-Z]]?)#oldName#([^[a-zA-Z]]?|$)","\1#newName#\2","ALL")> <cfset oldname="Ben"> <cfset newname="Mike"> <cfset newStr=rereplace(newStr,"(^|[^[a-zA-Z]]?)#oldName#([^[a-zA-Z]]?|$)","\1#newName#\2","ALL")> <!--- second one to handle ben ben as per ben ben doom ---> <cfset oldname="Ben"> <cfset newname="Mike"> <cfset newStr=rereplace(newStr,"(^|[^[a-zA-Z]]?)#oldName#([^[a-zA-Z]]?|$)","\1#newName#\2","ALL")> Jerry Johnson >>> [EMAIL PROTECTED] 06/04/03 11:12AM >>> Nice touch. Handles the two most obvious cases. OK, I made a mistake, which explains the "ben ben" non-find. Get rid of the ? in both [^[a-zA-Z]] checks. <cfset oldname="ben"> <cfset newname="mike> <cfset newStr=rereplace(str,"(^|[^[a-zA-Z]])#oldName#([^[a-zA-Z]]|$)","\1#newName#\2","ALL")> <cfset oldname="Ben"> <cfset newname="Mike"> <cfset newStr=rereplace(newStr,"(^|[^[a-zA-Z]])#oldName#([^[a-zA-Z]]|$)","\1#newName#\2","ALL")> Jerry Johnson >>> [EMAIL PROTECTED] 06/04/03 11:01AM >>> Standing on the shoulders of giants a very quick and dirty way of dealing with the case issue: <cfset oldname="ben"> <cfset newname="mike> <cfset t=rereplace(str,"(^|[^[a-zA-Z]]?)#oldName#([^[a-zA-Z]]?|$)","\1#newName# \2","ALL")> <cfset oldname="Ben"> <cfset newname="Mike"> <cfset t=rereplace(str,"(^|[^[a-zA-Z]]?)#oldName#([^[a-zA-Z]]?|$)","\1#newName# \2","ALL")> i.e. not using REReplaceNoCase, just REReplace. AndrT -----Original Message----- From: Jerry Johnson [mailto:[EMAIL PROTECTED] Sent: 04 June 2003 15:46 To: CF-Talk Subject: Re: Regex replacing whole words This gets you part of the way: <cfset oldname="ben"> <cfset str="Ben lives in benland. But ben is actually not called ben. He's called ben-dabble."> <cfset newname="mike"> <cfset t=rereplaceNoCase(str,"(^|[^[a-zA-Z]]?)#oldName#([^[a-zA-Z]]?|$)","\1#ne wName#\2","ALL")> <cfoutput>#t#</cfoutput> Note that it does not retain the proper capitalization from the original, but instead uses the name as supplied by newName. To get the proper capitalization, I think you will either need to run through every permutation of capitalization as different case-specific regex, or do a find-and-replace instead. Ninja, any ideas here? Jerry Johnson >>> [EMAIL PROTECTED] 06/04/03 10:34AM >>> I can't get my head around regular expressions! Help me please before I go mad! I need to replace one word with another, matching cases (if possible), but in a smart way. eg. "Ben lives in benland. But ben is actually not called ben. He's called ben-dabble" Say I want to replace 'ben' (any case) with 'mike', I would like to get: "Mike lives in benland. But mike is actually not called mike. He's called mike-dabble" Note the punctuation - I only want whole words, or words which are surrounded by punctuation. Does that make sense? I know I'm asking a lot but doing it using normal code and replace() is a slow operation. Even a reg-ex which will just replace whole words (including if they're at the beginning or end of a string) would be fantastic. I could do 2 regexs to do the case-matching, if it's not possible to do it in 1. Many Thanks! --------------------------------- Yahoo! Plus - For a better Internet experience ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4 Subscription: http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4 FAQ: http://www.thenetprofits.co.uk/coldfusion/faq This list and all House of Fusion resources hosted by CFHosting.com. The place for dependable ColdFusion Hosting. http://www.cfhosting.com Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4

