LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-01-2005, 06:08 PM   #1
ludeKing
Member
 
Registered: Jul 2003
Location: NSW. Australia
Distribution: Ubuntu, Fedora, Slackware
Posts: 181

Rep: Reputation: 30
simple regex not so simple (perl)


Hi all,
I am having trouble with a substitution regex.

I have a string where I need all occurrences of certain string replaced with another.

ie replace all [cor] with <font color="red">

At the moment I have this:
Code:
$search_string = '[cor]';
$replace_string = '<font color="red">';

print $q->br("<br>Searching for $search_string...");

if ($processing_file =~ /\Q$search_string\E/) { 
     print $q->br("Found! Converting..."); 
     $processing_file =~ s/\Q$search_string\E/\Q$replace_string\E/g;
} else { 
     print $q->br("$search_string not found, moving on...");
}
and in a text file of:
Code:
some normal text
[cor] preceding tag should be changed

I get these results:
Code:
some normal text 
\<font\ color\=\"red\"\> preceding tag should be changed

its automatically escaping all the characters I need! How can I get around this? I've been trying for days!

If I escape the < "> characters before I perform the regex, I just get multiple escape characters!


p.s. the \Q and \E modifiers quote whats in the string and it seems to go ok, so thats not the problem I don't think.


confused:
 
Old 03-01-2005, 08:16 PM   #2
susefan
Member
 
Registered: Feb 2005
Location: California, USA
Distribution: Novell SuSE
Posts: 39

Rep: Reputation: 15
Lightbulb

Sorry, I'm not much of a PERL hacker,
but, if you can open the file in vi (or vim),

like this:

vi filename

then enter the following (just copy and paste)
:1,$s/\[cor\]/<font color="red">/g
and press <RETURN>

seems to work for me.

Hope this helps.
 
Old 03-01-2005, 08:24 PM   #3
ludeKing
Member
 
Registered: Jul 2003
Location: NSW. Australia
Distribution: Ubuntu, Fedora, Slackware
Posts: 181

Original Poster
Rep: Reputation: 30
Hi thanks for the reply,

but its a CGI script that I am building that will get a bunch of search expressions from an array and replace them with the equivalent html, so its got to be automated.
(although the example I gave was a static one, I'll extend it when i get it going!)

Cheers though!

Last edited by ludeKing; 03-01-2005 at 08:28 PM.
 
Old 03-01-2005, 11:25 PM   #4
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Can you try:
$replace_string = '\'<font color="red">\'';
or
$replace_string = "'<font color=\"red\">'";
?
 
Old 03-02-2005, 01:15 AM   #5
farmerjoe
Member
 
Registered: Oct 2004
Location: Texas
Distribution: Ubuntu - Home, RHEL4 - Server
Posts: 96

Rep: Reputation: 15
I may be misunderstanding the question, but can you not use something like this?

perl -pi -e 's/[c]/<font color=\"red\">/g' $FILENAME

That should replease every occurence of [c] with <font color="red">

Sorry if i have misunderstood the problem.

-farmerjoe
 
Old 03-02-2005, 02:29 AM   #6
ludeKing
Member
 
Registered: Jul 2003
Location: NSW. Australia
Distribution: Ubuntu, Fedora, Slackware
Posts: 181

Original Poster
Rep: Reputation: 30
thanks for the reply also, I just got it actually.
Code:
$search_string = "[cor]";
		$replace_string = "<font color=\"red\">";

		
		print $q->br("<br>Searching for $search_string...");
				
		if ($processing_file =~ /\Q$search_string\E/) {
			print $q->br("Found! Converting...");
			
            $processing_file =~ s/\Q$search_string\E/$replace_string/g;
so i had to escape the quotes in the html and then quote the square brackets! argh!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
simple perl and regex phlx Programming 6 12-03-2004 03:01 PM
simple Perl question true_atlantis Programming 5 11-18-2003 08:37 PM
Simple Perl Question jacksmash Programming 1 11-13-2003 09:17 PM
Simple Perl Question Danny.au Programming 6 07-18-2002 09:46 AM
simple perl tunedLow Programming 4 06-04-2002 03:08 PM


All times are GMT -5. The time now is 04:19 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration