regular express replace?

Discussion in 'Perl' started by nick, Jun 5, 2004.

  1. nick

    nick Guest

    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?
     
    nick, Jun 5, 2004
    #1
    1. Advertising

  2. 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
     
    Gunnar Hjalmarsson, Jun 5, 2004
    #2
    1. Advertising

  3. 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
     
    Jürgen Exner, Jun 5, 2004
    #3
  4. nick

    mp3idiot Guest

    nick <> wrote in message news:<c9rtjq$i6u$>...
    > 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/;
    }
     
    mp3idiot, Jun 13, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stitch Jones
    Replies:
    4
    Views:
    584
    Stitch Jones
    Aug 22, 2004
  2. ad
    Replies:
    7
    Views:
    687
    Scott Allen
    Apr 11, 2005
  3. =?Utf-8?B?Q2FybCBNLg==?=
    Replies:
    4
    Views:
    2,495
    sivlookingforu
    Aug 7, 2006
  4. brooksr
    Replies:
    3
    Views:
    339
    Jon Paal
    Dec 24, 2006
  5. Jake Henderson

    Visual Web Developer 2005 Express and SQL 2005 Express

    Jake Henderson, Mar 10, 2006, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    249
    Jake Henderson
    Mar 10, 2006
Loading...

Share This Page