LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   simple regex not so simple (perl) (https://www.linuxquestions.org/questions/programming-9/simple-regex-not-so-simple-perl-296460/)

ludeKing 03-01-2005 06:08 PM

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:

susefan 03-01-2005 08:16 PM

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.

ludeKing 03-01-2005 08:24 PM

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!

95se 03-01-2005 11:25 PM

Can you try:
$replace_string = '\'<font color="red">\'';
or
$replace_string = "'<font color=\"red\">'";
?

farmerjoe 03-02-2005 01:15 AM

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

ludeKing 03-02-2005 02:29 AM

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!


All times are GMT -5. The time now is 05:43 AM.