in reply to use regex to split sentence


The following regex should do what you require. It uses a zero-width positive look-ahead as explained in perlre.     @match = /X\d.+?(?=X\d|$)/g;

Also, following on from jryan's idea here is a version that uses split:

$_ = "X1aaX2bbX3heeX4...X5loveUX6XXXX7X8" ; my @match = split /(X\d)/; print "String: " . $_; foreach my $word (@match){ print $word; print "\n" unless $word =~ /X\d/; }

--
John.

Replies are listed 'Best First'.
Re: Re: use regex to split sentence
by jryan (Vicar) on Nov 01, 2002 at 18:17 UTC

    Or, improving on both of ours, just:

    @match = split (/(?=X\d)/,$_);

    For some retarded reason last night, I thought that split "kept" the part that it split on, forgetting that the trick to do that was to use a lookahead. :)

Re: Re: use regex to split sentence
by kelan (Deacon) on Nov 02, 2002 at 05:08 UTC

    In Perl6:

    ### <Perl6> @match = m:e/ X \d : .+? <before X \d | $ > /; ### </Perl6>
    I do think they should give shortened versions of the <before > and <after > assertions. Oh well, maybe they'll throw it in.

    (Note for onlookers: The colon within the regex is thrown in for a small bit of engine optimization, and actually also helps in detecting malformed strings.)

    kelan


    Perl6 Grammar Student