in reply to perl html tag editing to uppercase

$lines=~m/(<+)(\/?)(.+)(>+)/

This unfortuately is not the right approach. You regex will match quite a bit more than you expect.

You should look into a module like HTML::TokeParser.

Now getting back to why your regex wasn't helping:
m/(<+)(\/?)(.+)(>+)/
  • (<+) - capture one or more occurances of '<' so << or <<<<<<<<< will match and be captured to $1
  • (\/?) - capture zero or one '/' to $2. Not that this matters because if you use the uc function instead of your tr/// the '/' will be ignored
  • (.+) - capture 1 or more of anything but a line ending to $3. NOTE: this is greedy and will gobble everything in site unless it's required to finish the match
  • (>+) - capture one or more '>' (like >> or >>>>) to $4. NOTE: because the the '.+' is greedy you can have some very strange results with 2 '>' on a line whether or not it is in a tag.
  • imagine this line of HTML

    look at this <<<----- <b>It is really neat</b> I swear >>

    What will be captured to $3 is

    ----- <b>It is really neat</b> I swear

    One tool to add to your regex arsenal is negated character classes like [^ ]. Rewriting your regex like /<([^>]+)>/ would get you closer, but still is not what you want. You can read more about negated character classes in perlre

    Writing a HTML parser with a regex is a somewhat like hammering a nail with a flounder. You should really look for right tool in perl, in this case a true parser is going to help you out a lot.



    grep
    grep> cd /pub
    grep> more beer

    Replies are listed 'Best First'.
    Re: Re: perl html tag editing to uppercase
    by Anonymous Monk on Apr 11, 2002 at 08:30 UTC
      Thankyou for your help :) I found a oneliner that doesn't do the whole job but still
      $lines =~ s/<(.+?)>/'<'.uc($1).'>'/ge;
      This will convert src attributes such as blah.gif to uppercase which would wreak havock on some servers but its only an exercise to drive me nuts :) Else I could:
      if ($lines=~m/(<)(.+)(>)/) { $tag=$1; $tag2=$2; $tag3=$3; $tag2=~tr/a-z/A-Z/; $new=$tag.$tag2.$tag3; } $upper=~s/(<)(.+)(>)/$new/g;
      Boy am I hangin to learn about modules now :)