Re: tough regex problem

Ramki <[email protected]>
Newsgroups gmane.comp.lang.perl.fun
Message-ID <[email protected]>
suboptimal, but works correctly for all input files below.
#-------------------------------
return unless wantarray;
return unless $$buf_ref =~ /./;
return (${$buf_ref} =~ /.*?(?:$sep{2}|\z)/gs) if $paragraph_mode;
return (${$buf_ref} =~ /.*?(?:$sep|\z)/gs );
#-------------------------------

test input files generated as follows:
	perl -e 'print' >empty

	#NOTE: para 2 begins with a newline below, para3 has no $/ at end
	perl -e "print qq{\nPara1\n\n\nPara2\n\nPara3_NOEND}" >para_test

	perl -le 'print for map { "line$_" } 0..5' >5lines

-- Ramki


On Fri, Sep 17, 2004 at 02:05:52AM -0400, Uri Guttman wrote:
> 
> in file::slurp i currently split the file into lines (or records based
> on $/) using this code:
> 
> 	return split( m|(?<=$sep)|, ${$buf_ref} ) if wantarray ;
> 
> where $sep is set from $/.
> 
> that works correctly and is fast. but it has one flaw, it can't do
> paragraph mode since lookbehind can't handle variable width regexes as
> \n\n+ is. so i am looking for an m// solution that properly splits a
> buffer into records. it is not as easy as it seems. it has to behave
> exactly like the split does. there are several corner cases such as an
> empty file and a partial line ending a file. even a normal file of lines
> is tricky as you don't want to return an bogus empty line after the last
> real line. here are a few broken attempts to help you avoid pitfalls:
> 
> 	/(.*?$sep|.+)/sg	# doesn't handle an empty file
> 	/.*?(?:$sep|\z)/gs 	# adds a bogus empty line at the end
> 
> i already passed this problem to a top regex person and he sent me the
> second one above (obviously untested! :).
> 
> so fame, glory and an acknowledgement in the new release all go to the
> valiant warrior who solves this.
> 
> let the fl^Wgames begin!
> 
> uri
> 
> -- 
> Uri Guttman  ------  [email protected]  -------- http://www.stemsystems.com
> --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
> Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org

-- 
----------------------------------------------------------------
Let us see how high we can fly before the sun melts the wax 
in our wings.  -- Subrahmanyam Chandrasekhar
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.