regular express replace?

N

nick

Hello, how to use the perl regular express to do:

In a html file, replace all the underscore "_" to "-" for the alt
attribute of <area> tags (only for the alt of area tag)? And convert the
alt attribute to uppercase.

For example:

<area shape="rect" alt="abc_123"> will be converted to
<area shape="rect" alt="ABC-123">

Can this be done in one step?
 
G

Gunnar Hjalmarsson

nick said:
Hello, how to use the perl regular express to do:

In a html file, replace all the underscore "_" to "-" for the alt
attribute of <area> tags (only for the alt of area tag)? And
convert the alt attribute to uppercase.

This is homework, right? What have you tried?
 
J

Jürgen Exner

nick said:
Hello, how to use the perl regular express to do:

That is "regular expression", not "regular express". It surely has nothing
to do with "fast" ;-)
In a html file, replace all the underscore "_" to "-" for the alt
attribute of <area> tags (only for the alt of area tag)? And convert
the alt attribute to uppercase.

Contrary to popular believe parsing HTML correctly is close to rocket
science and while it may be possible to do so with REs alone, nobody in his
right mind would try it.
Use an HTML parser to parse the HTML code, further details please see the
FAQ "perldoc -q html": "How do I remove HTML from a string?"
Then it is trivial to isolate the correct attribute values and do the
necessary replacing. But even then I wouldn't use a regular expression
because there are better, more targeted functions:
- perldoc -f uc
- perldoc -f tr
For example:

<area shape="rect" alt="abc_123"> will be converted to
<area shape="rect" alt="ABC-123">

Can this be done in one step?

Once you have isolated the attribute value, then yes. But why?

jue
 
M

mp3idiot

nick said:
Hello, how to use the perl regular express to do:

In a html file, replace all the underscore "_" to "-" for the alt
attribute of <area> tags (only for the alt of area tag)? And convert the
alt attribute to uppercase.

For example:

<area shape="rect" alt="abc_123"> will be converted to
<area shape="rect" alt="ABC-123">

Can this be done in one step?

if ( /<.*?alt="(.*?)">/ )
{
$alt_info = $1;
$alt_info_new = $alt_info;
$alt_info_new =~ tr /_a-z/-A-Z/;
s/$alt_info/$alt_info_new/;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top