Re: Regex replacing whole words
> That's great Jerry! Thanks! That does most of what I need. > I think the majority of the 'case' problems will just be > where the first letter is capitalised. > One thing I've noticed though is that if you have 'ben > ben' it only replaces the first 'ben' with 'mike'. So it > becomes 'mike ben'. > Weird. > But thanks for the efforts, that's cool! Yea, what happens is that it sees the first instance of " ben "... but because the space on the end of that is also part of the space on the beginning of the next " ben " it doesn't see the 2nd iteration because the string it's looking at begins with "b" not " ". So if you want to get them all even when they're back to back, you have to run the same regex twice. hth s. isaac dealey972-490-6624 new epoch http://www.turnkey.to lead architect, tapestry cms http://products.turnkey.to tapestry api is opensource http://www.turnkey.to/tapi certified advanced coldfusion 5 developer http://www.macromedia.com/v1/handlers/index.cfm?ID=21816 ~| 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 Get the mailserver that powers this list at http://www.coolfusion.com Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
RE: Regex replacing whole words
The extra brackets were a left over from changing from POSIX alpha class to a-z style. And the one you have there works. (I am going back to writing verity search97 indexing routines - you know, something simple.) Jerry Johnson >>> [EMAIL PROTECTED] 06/04/03 11:26AM >>> Now that I look more closely, the ? is wrong (good catch!) and you've got too many brackets. However, that still doesn't fix the benben problem, as I explained in another email. The re should be (^|[^a-zA-Z])#oldName#([^a-zA-Z]|$) Note that the a-zA-Z is not in a second set of brackets. -- Ben Doom Programmer & General Lackey Moonbow Software, Inc : -Original Message- : From: Jerry Johnson [mailto:[EMAIL PROTECTED] : Sent: Wednesday, June 04, 2003 11:12 AM : To: CF-Talk : Subject: RE: Regex replacing whole words : : : 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. : : : : : : : : : 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: : : : : : : : : : 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: : : : : : : : : #t# : : : 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 Get the mailserver that powers this list at http://www.coolfusion.com Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
RE: Regex replacing whole words
No -- if you leave the ? in, then benbob will match because that's the same as ben(empty string)bob which matches. -- Ben Doom Programmer & General Lackey Moonbow Software, Inc : -Original Message- : From: Jerry Johnson [mailto:[EMAIL PROTECTED] : Sent: Wednesday, June 04, 2003 11:19 AM : To: CF-Talk : Subject: RE: Regex replacing whole words : : : My recent suggestion didn't work. : : Leave the ? in there! : : : : : : : : : : : : : : : : : : 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. : : : : : : : : : 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: : : : : : : : : : 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: : : : : : : : : #t# : : : 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
RE: Regex replacing whole words
Now that I look more closely, the ? is wrong (good catch!) and you've got too many brackets. However, that still doesn't fix the benben problem, as I explained in another email. The re should be (^|[^a-zA-Z])#oldName#([^a-zA-Z]|$) Note that the a-zA-Z is not in a second set of brackets. -- Ben Doom Programmer & General Lackey Moonbow Software, Inc : -Original Message- : From: Jerry Johnson [mailto:[EMAIL PROTECTED] : Sent: Wednesday, June 04, 2003 11:12 AM : To: CF-Talk : Subject: RE: Regex replacing whole words : : : 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. : : : : : : : : : 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: : : : : : : : : : 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: : : : : : : : : #t# : : : 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 Your ad could be here. Monies from ads go to support these lists and provide more resources for the community. http://www.fusionauthority.com/ads.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
RE: Regex replacing whole words
My recent suggestion didn't work. Leave the ? in there! 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. 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: 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: #t# 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
RE: Regex replacing whole words
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. 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: 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: #t# 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 Host with the leader in ColdFusion hosting. Voted #1 ColdFusion host by CF Developers. Offering shared and dedicated hosting options. www.cfxhosting.com/default.cfm?redirect=10481 Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
RE: Regex replacing whole words
This will work for the titlecase and whatnot. You'll still need to run the rereplaces twice each to handle the dreaded "ben ben" problem. But it's basically a good approach. -- Ben Doom Programmer & General Lackey Moonbow Software, Inc : -Original Message- : From: Andre Mohamed [mailto:[EMAIL PROTECTED] : Sent: Wednesday, June 04, 2003 11:01 AM : To: CF-Talk : Subject: RE: Regex replacing whole words : : : Standing on the shoulders of giants a very quick and dirty way of : dealing with the case issue: : : : : : : : : : i.e. not using REReplaceNoCase, just REReplace. : : André : : : -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: : : : : : : : : #t# : : : 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 Host with the leader in ColdFusion hosting. Voted #1 ColdFusion host by CF Developers. Offering shared and dedicated hosting options. www.cfxhosting.com/default.cfm?redirect=10481 Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
RE: Regex replacing whole words
As far as the "ben ben" problem goes -- this is a problem I've run into before. Essentially, the space after the first ben is "used up" so it can't be used to match the next "ben". So you have to run the replace multiple times. By the way, I approve of your example names. :-) Anyhow, here's what I'd do to solve the benben problem as well as the capitalization problem: a loop. something like... refindnocase of first instance of oldname while (there's an instance found) check the case of the instance replace with appropriately capped version of newname find next instance end loop If you're just worried about titlecase, you could use something like refind on "^[A-Z]" to determine if the first letter is capital. If you are also worried about all cap, you could also test something like refind on "[a-z]" to see if there are any lowercase. HTH. If you need more assistance, or this doesn't make sense, or you think I'm a Crack Smoking Monkey for thinking this way, feel free to ask/vent at: http://www.houseoffusion.com/cf_lists/index.cfm?method=threads&forumid=21 The HOFREGEX list. For all your abbreviation (and RE) needs. -- Ben Doom Programmer & General Lackey Moonbow Software, Inc : -Original Message- : From: Jim Banks [mailto:[EMAIL PROTECTED] : Sent: Wednesday, June 04, 2003 10:54 AM : To: CF-Talk : Subject: Re: Regex replacing whole words : : : That's great Jerry! Thanks! That does most of what I need. I : think the majority of the 'case' problems will just be where the : first letter is capitalised. : : One thing I've noticed though is that if you have 'ben ben' it : only replaces the first 'ben' with 'mike'. So it becomes 'mike ben'. : : Weird. : : But thanks for the efforts, that's cool! : : : : : - : 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 Get the mailserver that powers this list at http://www.coolfusion.com Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
RE: Regex replacing whole words
Standing on the shoulders of giants a very quick and dirty way of dealing with the case issue: i.e. not using REReplaceNoCase, just REReplace. André -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: #t# 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 Host with the leader in ColdFusion hosting. Voted #1 ColdFusion host by CF Developers. Offering shared and dedicated hosting options. www.cfxhosting.com/default.cfm?redirect=10481 Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Re: Regex replacing whole words
That's great Jerry! Thanks! That does most of what I need. I think the majority of the 'case' problems will just be where the first letter is capitalised. One thing I've noticed though is that if you have 'ben ben' it only replaces the first 'ben' with 'mike'. So it becomes 'mike ben'. Weird. But thanks for the efforts, that's cool! - 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 Host with the leader in ColdFusion hosting. Voted #1 ColdFusion host by CF Developers. Offering shared and dedicated hosting options. www.cfxhosting.com/default.cfm?redirect=10481 Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Re: Regex replacing whole words
This gets you part of the way: #t# 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 Your ad could be here. Monies from ads go to support these lists and provide more resources for the community. http://www.fusionauthority.com/ads.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4