SPUG: Lookahead RE with split() !
Tim Maher/CONSULTIX
tim at consultix-inc.com
Mon Nov 29 15:41:10 CST 1999
SPUGers,
Continuing with my reading of the Perl Cookbook, p. 177 of the
Pattern Matching chapter mentions that you can use a lookahead
RE with split(), to split before a pattern, without consuming
it (as opposed to consuming a delimiting pattern, like usual).
Here's an example I "cooked up", that will undoubtedly make an
appearance in my "Advanced Pattern Matching with Perl" class on
12/17:
$ cat look_split
#! /usr/bin/perl -w
# lookahead_split
$_=join '', <DATA>; # read data from below __DATA__
$,=', '; # show comma-space between print arguments
# using split's match as delimiter
print "First:\n"; print split /[A-Z]\w+:/;
# using split's match as returned delimiter
print "\nSecond:\n"; print split /([A-Z]\w+:)/;
# using split's lookahead match as delimiter
print "\nThird:\n"; print split /(?=[A-Z]\w+:)/;
__DATA__
Smith: 42A Wesson: 19B Rex: 0 Philbert: 124C
$ ./look_split
First:
, 42A , 19B , 0 , 124C
Second:
, Smith:, 42A , Wesson:, 19B , Rex:, 0 , Philbert:, 124C
Third:
Smith: 42A , Wesson: 19B , Rex: 0 , Philbert: 124C
$
Now I'm trying to remember how I ever managed to function without
lookahead-REs (in earlier Perl versions)!
*============================================================*
| 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/7 UNIX Fund; 12/13 Perl+Modules; 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