On Aug 10, Michel Lambert said:

>Fun with Perl's Regexes, to be more precise. They're a lot more fun than
>regular regexes. Here's a little quiz I made up while falling asleep last
>night.

Mike, I'd call you sick, but I'd hate to think what adjective that would
leave for me. ;)

>1) What's the fastest way to strip trailing whitespace ( as defined by
>/\s/ ) from a decent-sized string? Decent-sized means this email, or 100 of
>these emails concatenated together.

I'm standing behind my sexeger approach:

  $_ = reverse;
  s/^\s+//;
  $_ = reverse;

The reason s/\s+$// is so bad is because it isn't optimized, and therefore
the \s+ matches at EVERY chunk of whitespace in your string, no matter
where it is, before it gets to the very end.  Ick-o.

>2) Is it possible to write a function which can tell me if a regex matches
>the empty string? (Since the code would most likely be long, a simple why or
>why not will do ;)

I stand behind the

  sub empty { "" =~ shift }

approach.  But, if you mean "can you inspect a regex and determine if it
matches the empty string", I, being a regex junkie, will say yes.

Here are the following ways a regex can match an empty string:

  * all of its tokens have a {0,X} quantifier
    ? and * and {0,4}

  * it contains no text-matching tokens
    /^$/ matches ""

  * it contains alternation to an empty string at each level
    /(abcd|)efgh|/

I think that covers it.  And that can be determined in parsing.

>3) Write a regex that uses variable-width-lookahead without using the
>various look-ahead operators, (?=..), (?!..), (?<=..), or (?<!..)

If Perl wouldn't be such a hag about re-entering the engine, I'd do it in
a second:

  (?=...)   (?(?{ $' =~ /\A.../ })|(?!))
  (?!...)   (?(?{ $' =~ /\A.../ })(?!))
  (?<=...)  (?(?{ "$`$&" =~ /...\z/ })|(?!))
  (?<!...)  (?(?{ "$`$&" =~ /...\z/ })(?!))

I'd prefer to use sexeger approaches for those last two, but that would be
getting ahead of myself. ;)

Oh, and don't yell at me for using (?!) as the failure.  I suppose I could
have put something heinous in there like -\A instead (I'd like to see YOU
match a dash before the beginning of a string!).

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **

Reply via email to