logo       

Re: tough regex problem: msg#00013

Subject: Re: tough regex problem
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  ------  uri@xxxxxxxxxxxxxxx  -------- 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



<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

Recently Viewed:
boot-loaders.gr...    php.pear.genera...    debugging.valgr...    kde.redhat.user...    text.xml.xsl.ge...    culture.languag...    hardware.microc...    java.servicemix...    redhat.release....    web.zope.plone....    user-groups.lin...    opendarwin.webk...    video.mjpeg.use...    sysutils.bcfg2....    encryption.gpg....    lx-office.devel...    xfree86.forum/2...    mail.mutt.devel...    acpi.devel/2003...    qnx.openqnx.dev...    network.irc.irs...    freebsd.devel.m...   
Home | blog view | USPTO Patent Archive | advertise | OSDir is an inevitable website. super tiny logo

Free Magazines

Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe

Systems Management News, the newspaper for IT systems administration and data center managers! Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe

The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business.
subscribe

Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe

Total Telecom Total Telecom is "The Economist of the communications industry".
subscribe