| | |
matching and replacing workaround
Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: May 2005
Posts: 45
Reputation:
Solved Threads: 0
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
Thanks,
Derek
•
•
Join Date: Feb 2008
Posts: 33
Reputation:
Solved Threads: 1
1
#2 Nov 9th, 2009
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
Give this a try
perl Syntax (Toggle Plain Text)
$someText =~ s/$searchstring/<font color=#FFFFFF>$&<\/font>/gi;
•
•
•
•
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
•
•
Join Date: May 2005
Posts: 45
Reputation:
Solved Threads: 0
0
#3 Nov 10th, 2009
•
•
•
•
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)
$someText =~ s/$searchstring/<font color=#FFFFFF>$&<\/font>/gi;
wow! So simple! This worked PERFECTLY! Thanks a million!
![]() |
Similar Threads
- Strings - Replacing a word with another word (C++)
- Ide #1 Error after replacing a dvd with an dvd/cd-rw (Storage)
- another newbie with alot of redhat and apache server Q'S (Linux Servers and Apache)
- thumb impression matching using VC++ (C++)
- Using Matching (Perl)
- I've got Trojan.Holax... is this bad? (Viruses, Spyware and other Nasties)
- not-a-virusadware (Viruses, Spyware and other Nasties)
- minimum weight perfect matching (Computer Science)
- Finding and Replacing Strings (C++)
Other Threads in the Perl Forum
- Previous Thread: suggestions for writing automation script
- Next Thread: parsing multiple files in current directory
Views: 715 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Perl





