in reply to Angle brackets and regex problem

You've told it to match one character not an > (which 1 is), one or more digits (which 3 is), zero or more lower case letters (which nothing is), and one character that's not < (which a is). Wherein lies the problem?

And of course there's the standard jibe that in general you want to use something like HTML::TreeBuilder or the like to parse HTML, not regexen.

Update: Curse you, Red Baronmerlyn. :)

And a hopefully useful pointer: YAPE::Regex::Explain can be helpful for obtaining a prose explanation of just what your regex means. Using it on your regex produces this:

perl -MYAPE::Regex::Explain -le 'print YAPE::Regex::Explain->new( qr/( +[^>]\d+[a-z]*[^<])/ )->explain' The regular expression: (?-imsx:([^>]\d+[a-z]*[^<])) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- [^>] any character except: '>' ---------------------------------------------------------------------- \d+ digits (0-9) (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- [a-z]* any character of: 'a' to 'z' (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- [^<] any character except: '<' ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------

Replies are listed 'Best First'.
Re^2: Angle brackets and regex problem
by emav (Pilgrim) on Jan 05, 2007 at 17:36 UTC
    It's official! I'm blind! :(

    I expected it not to find a match. Oh, dear! So embarrassing!

      If you ever wonder what something matched, you can use

      my $at = $-[0]; my $matched = substr($test, $at, $+[0]-$at); warn(Matched "$matched" at $at\n");

      Alternatively, you could pass the argument -Meval=debug to perl. (e.g. perl -Mre=debug script.pl). It produces a detailed (i.e. long) report of all regexp activities.

      References:

      • @- and @+ are documented in perlvar.
      • -Mre=debug adds use re 'debug'; to your program. See re.