SPUG: Awful Recipes in Cookbook

Tim Maher/CONSULTIX tim at consultix-inc.com
Sun Dec 26 14:02:13 CST 1999


SPUGsters,

The closer I look at the Perl Cookbook, the less I like it.  (But I've
got the first printing, for which many errata have never been officially
identified or corrected.)

In particular, page 172, which shows a "general construct" for finding
the last (rightmost) match for a pattern.

In my edition, the formula is shown as A(?!.*A)*$, which definitely
isn't right.  Then in their /x-expansion of that same formula, in the next
paragraph, they show A(?!.*A)$ (no asterisk before the $), which requires
the initial A to be at the end of the line!  Still not right. Finally,
in the real code example, they use A(?!.*A), which actually works, but
still without a decent explanation.

I'm developing some new lookahead/lookbehind examples for my "Advanced
Pattern Matching" course, and finding that the Cookbook has the most
detailed and interesting coverage - but there are no real explanations,
and as I've indicated the examples are chock full of errors.

I'm especially interested in explanations of examples that trick the
regex engine into "scanning forward" by matching text repeatedly within
non-capturing parentheses, of the basic form A((?:(?!A).)*) (which,
having matched the initial A, apparently advances one character at a
time ensuring that none of the following sequences matches an A).

Does anybody know of any decent documentation on how lookahead/lookbehind
work in Perl, or any practical examples?  The Perl FAQs, perlre man
page, Cookbook, Mastering Regular Expressions, Camel, and Effective Perl
Programming generally give very simplistic examples and explanations,
or none at all.  As a course developer, I'm not unaccustomed to being the
first to really try to explain how something works, but when there's hidden
"magic" involved (as with A((?:(?!A).)*)), I could use some help in
figuring out what's going on!

Has anybody actually found good uses for lookbehind?  The Perl
implementation seems so limited as to be nearly worthless to me, but
maybe I'm missing something.

*==================================================================*
| Tim Maher, PhD  Consultix &        (206) 781-UNIX/8649           |
|  Pacific Software Gurus, Inc       Email: tim at consultix-inc.com  |
|  UNIX/Linux & Perl Training        http://www.consultix-inc.com  |
| Classes: 12/13 Perl; 12/17 Adv. Pattern Matching; 1/18 Int. Perl |
*==================================================================*

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
        Email to majordomo at pm.org: ACTION spug-list your_address





More information about the spug-list mailing list