Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > regular express replace?

Reply
Thread Tools

regular express replace?

 
 
nick
Guest
Posts: n/a
 
      06-05-2004
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?
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      06-05-2004
nick wrote:
> 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?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      06-05-2004
nick wrote:
> 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


 
Reply With Quote
 
mp3idiot
Guest
Posts: n/a
 
      06-13-2004
nick <(E-Mail Removed)> wrote in message news:<c9rtjq$i6u$(E-Mail Removed)>...
> 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/;
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular express question elca Python 1 11-02-2009 03:23 AM
using regular express to analyze lisp code Kelie Python 4 10-04-2007 06:34 PM
using regular phone as voip device with regular modem? hygum VOIP 5 03-23-2005 03:51 PM
Regular Express to remove all text between to ,'s Stitch Jones Perl 4 08-22-2004 09:47 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments