|
Posted by PerlFAQ Server on March 18, 2008, 9:03 am
Please log in for more thread options
This is an excerpt from the latest version perlfaq6.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .
--------------------------------------------------------------------
6.4: I put a regular expression into $/ but it didn't work. What's wrong?
$/ has to be a string. You can use these examples if you really need to
do this.
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).
local $_ = "";
while( sysread FH, $_, 8192, length ) {
while( s/^((?s).*?)your_pattern/ ) {
my $record = $1;
# do stuff here.
}
}
You can do the same thing with foreach and a match using the
c flag and the \G anchor, if you do not mind your entire file
being in memory at the end.
local $_ = "";
while( sysread FH, $_, 8192, length ) {
foreach my $record ( m/\G((?s).*?)your_pattern/gc ) {
# do stuff here.
}
substr( $_, 0, pos ) = "" if pos;
}
--------------------------------------------------------------------
The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.
If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.
|
| Similar Threads | Posted | | FAQ 6.4: I put a regular expression into $/ but it didn't work. What's wrong? | October 27, 2004, 5:03 pm |
| FAQ 6.4 I put a regular expression into $/ but it didn't work. What's wrong? | March 2, 2005, 12:03 am |
| FAQ 6.4 I put a regular expression into $/ but it didn't work. What's wrong? | April 27, 2005, 11:03 pm |
| FAQ 6.4 I put a regular expression into $/ but it didn't work. What's wrong? | July 13, 2005, 4:03 am |
| FAQ 6.4 I put a regular expression into $/ but it didn't work. What's wrong? | September 14, 2005, 10:03 pm |
| FAQ 6.4 I put a regular expression into $/ but it didn't work. What's wrong? | November 5, 2005, 5:03 pm |
| FAQ 6.4 I put a regular expression into $/ but it didn't work. What's wrong? | November 24, 2005, 11:03 pm |
| FAQ 6.4 I put a regular expression into $/ but it didn't work. What's wrong? | December 28, 2005, 11:03 am |
| FAQ 6.4 I put a regular expression into $/ but it didn't work. What's wrong? | May 6, 2006, 9:03 am |
| FAQ 6.4 I put a regular expression into $/ but it didn't work. What's wrong? | August 24, 2006, 3:03 am |
|