perlstu has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to extract lowercase characters out of a html tag convert it to uppercase then overwrite the code to the same file. It should look something like this:
if ( $fold =~m/.html$/) { open(FILE, $fold) || die "Cannot open File\n"; @lines=<FILE>; close (FILE); foreach $lines(@lines) { $lines=~s/http/https/g; #------------upper extent of HELP Here------------- + #convert html tags fromlower to upper #Whats wrong with match and extraction here ! if ($lines=~m/(<+)(\/?)(.+)(>+)/) {$tag=$3; $tag=~tr/a-z/A-Z/; } } #------------------------lower extent of help ----------- @log=@lines; open(FILE,">$fold") || die "cannot open file"; print FILE @lines; open(LOG,"+>>logfile.txt") || die "log file will not open!"; print LOG @log,"\n","#------newlog---------","\n"; close (LOG); close (FILE); } }

Not all code is present, some bits maybe missing from copy and paste

Replies are listed 'Best First'.
Re: perl html tag editing to uppercase
by grep (Monsignor) on Mar 31, 2002 at 07:34 UTC
    $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
      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 :)