944,008 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Marked Solved
  • Views: 1180
  • Perl RSS
Nov 8th, 2009
1

matching and replacing workaround

Expand Post »
I want to search "The tree is in the yard" for the word "the", then replace the word "the" with "<font color=#FFFFFF>the</font>", which I currently do with "s/$searchstring/<font color=#FFFFFF>$searchstring</font>/gi;" BUT I want to preserve the case of the word, so that in this example the first word of the sentence would be white, but would have the capital T preserved. How can I do this?


Thanks,
Derek
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
derekn is offline Offline
45 posts
since May 2005
Nov 9th, 2009
1
Re: matching and replacing workaround
What you need to use is one of the back-reference variables, specifically you should use "$&" since you essentially want to reuse each occurrence of your search string

Give this a try
perl Syntax (Toggle Plain Text)
  1. $someText =~ s/$searchstring/<font color=#FFFFFF>$&<\/font>/gi;


Click to Expand / Collapse  Quote originally posted by derekn ...
I want to search "The tree is in the yard" for the word "the", then replace the word "the" with "<font color=#FFFFFF>the</font>", which I currently do with "s/$searchstring/<font color=#FFFFFF>$searchstring</font>/gi;" BUT I want to preserve the case of the word, so that in this example the first word of the sentence would be white, but would have the capital T preserved. How can I do this?


Thanks,
Derek
Reputation Points: 10
Solved Threads: 1
Light Poster
klactose is offline Offline
43 posts
since Feb 2008
Nov 10th, 2009
0
Re: matching and replacing workaround
Click to Expand / Collapse  Quote originally posted by klactose ...
What you need to use is one of the back-reference variables, specifically you should use "$&" since you essentially want to reuse each occurrence of your search string

Give this a try
perl Syntax (Toggle Plain Text)
  1. $someText =~ s/$searchstring/<font color=#FFFFFF>$&<\/font>/gi;

wow! So simple! This worked PERFECTLY! Thanks a million!
Reputation Points: 10
Solved Threads: 0
Light Poster
derekn is offline Offline
45 posts
since May 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: suggestions for writing automation script
Next Thread in Perl Forum Timeline: Match phone number





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC