X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperlfaq6.pod;h=6b0f3bb9a49abab58bd8a00847ab650addf3210c;hb=4ad40acfc62db410aa4eb7654e17246f1fc97689;hp=168233bd1b569c5f29ee8f91246e312f83212834;hpb=788611b6a6a160290f10302fc348e5dff91edc6e;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perlfaq6.pod b/pod/perlfaq6.pod index 168233b..6b0f3bb 100644 --- a/pod/perlfaq6.pod +++ b/pod/perlfaq6.pod @@ -1,6 +1,6 @@ =head1 NAME -perlfaq6 - Regular Expressions ($Revision: 1.20 $, $Date: 2003/01/03 20:05:28 $) +perlfaq6 - Regular Expressions ($Revision: 1.27 $, $Date: 2004/11/03 22:52:16 $) =head1 DESCRIPTION @@ -151,7 +151,19 @@ Up to Perl 5.8.0, $/ has to be a string. This may change in 5.10, but don't get your hopes up. Until then, you can use these examples if you really need to do this. -Use the four argument form of sysread to continually add to +If you have File::Stream, this is easy. + + use File::Stream; + my $stream = File::Stream->new( + $filehandle, + separator => qr/\s*,\s*/, + ); + + print "$_\n" while <$stream>; + +If you don't have File::Stream, you have to do a little more work. + +You can use the four argument form of sysread to continually add to a buffer. After you add to the buffer, you check if you have a complete line (using your regular expression). @@ -354,7 +366,7 @@ created by Jeffrey Friedl and later modified by Fred Curtis. $/ = undef; $_ = <>; - s#/\*[^*]*\*+([^/*][^*]*\*+)*/|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#$2#gs + s#/\*[^*]*\*+([^/*][^*]*\*+)*/|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#defined $2 ? $2 : ""#gse; print; This could, of course, be more legibly written with the C modifier, adding @@ -395,11 +407,11 @@ whitespace and comments. Here it is expanded, courtesy of Fred Curtis. . ## Anything other char [^/"'\\]* ## Chars which doesn't start a comment, string or escape ) - }{$2}gxs; + }{defined $2 ? $2 : ""}gxse; A slight modification also removes C++ comments: - s#/\*[^*]*\*+([^/*][^*]*\*+)*/|//[^\n]*|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#$2#gs; + s#/\*[^*]*\*+([^/*][^*]*\*+)*/|//[^\n]*|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#defined $2 ? $2 : ""#gse; =head2 Can I use Perl regular expressions to match balanced text? @@ -745,17 +757,17 @@ Or like this: } Here's another, slightly less painful, way to do it from Benjamin -Goldberg: +Goldberg, who uses a zero-width negative look-behind assertion. - $martian =~ m/ - (?!<[A-Z]) - (?:[A-Z][A-Z])*? - GX - /x; + print "found GX!\n" if $martian =~ m/ + (?