PDA

View Full Version : [ubuntu] [SOLVED] regular expression help



uioreanu
December 11th, 2008, 03:06 PM
Hello,

I am a beginner in what regular expressions are concerned. What I want is to replace a string into a html file with another string. The string to be replaced is here


<td><a href="mailto:info@abc.net" class="topmenu">Email</a></td>

and it should be replaced with:


<td><a href="contact.php" class="topmenu">Contact</a></td>

so basically the href and the link text should be changed. I have tried using sed, who I call using this syntax:

sed -e 's/string_to_replace/string_to_replace/g' file.html

Thing is that I'm stuck in the search for the proper regular expression. Can any of you regexp gurus help me?

Thanks!
Calin

karlr42
December 11th, 2008, 03:32 PM
Why have you marked the thread as solved?
I would use a perl script like:

#!/usr/bin/perl
my $replacement = "<td><a href=\"contact.php\"class=\"topmenu\">Contact</a></td>"
open(FILE, +>file.html) or die($!); #change to match your file
foreach(<FILE>){
if(/mailto/){
$_ =~ s/.*/$replacement/;
}
}
close(FILE) or die($!);
Note I'm a bit rusty on perl, and there are probably much, much better ways of doing this.

rrashkin
December 11th, 2008, 04:43 PM
or in Python:

f=open(<your filename>)
for s1 in f:
if (s1.find("mailto:")>0 and s1.find("Email")>0):
s1=s1.replace('mailto:info@abc.net','contact.php')
s1=s1.replace('Email','Contact')
f.close()

glennric
December 11th, 2008, 04:58 PM
Or in Bash:

#!/bin/bash
sed -i -e 's/<td><a href="mailto:info@abc\.net" class="topmenu">Email<\/a><\/td>/<td><a href="contact.php" class="topmenu">Contact<\/a><\/td>/' test.html