tedv has asked for the wisdom of the Perl Monks concerning the following question:

Suppose I have a string that matches /^(\~?.)*$/ and I want to split it into each atomic piece that would be matched by (\~?.). How would I code such a regular expression for split? I'd like to use lookbehind, crafting an expression that says, "Split after every non-tilde character", but my O'Reilly Regular Expression book says that perl doesn't support lookbehind. Any suggestions?

-Ted

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I match this without lookbehind?
by Adam (Vicar) on Dec 08, 2000 at 05:50 UTC
    my @results = grep {$_} split /([~]*?[^~])/, $input;
Re: How do I match this without lookbehind?
by chipmunk (Parson) on Dec 08, 2000 at 08:10 UTC
    When the Regular Expressions book was published, the latest version of Perl was 5.005_03. As of 5.6.0, Perl does support lookbehind.

    However, you really don't need lookbehind or split to solve this problem: @matches = $string =~ /~?./g;

Re: How do I match this without lookbehind?
by runrig (Abbot) on Dec 08, 2000 at 06:00 UTC
    Sometimes split is not the best answer for splitting:
    my @results; push @results, $1 while /(~?.)/g;
Re: How do I match this without lookbehind?
by Shakya (Initiate) on Oct 04, 2007 at 03:31 UTC
    my @results = $string =~ m!([^~]+)!g;
Re: How do I match this without lookbehind?
by I0 (Priest) on Dec 12, 2000 at 19:26 UTC
    Perl v5.6.0 does suport lookbehind
    split /(?<=[^~])/
    or do you mean split /(?<=[^~])(?=~)/

    Originally posted as a Categorized Answer.

Re: How do I match this without lookbehind?
by I0 (Priest) on Dec 12, 2000 at 19:40 UTC
    Perl v5.6.0 does suport lookbehind
    split /(?<=[^~])/ #or split /(?<!~)/ #or /(~?.)/g
Re: How do I match this without lookbehind?
by runrig (Abbot) on Dec 08, 2000 at 06:19 UTC
    my @results; push @results, $1 while /(~?[^~])/g;

    Originally posted as a Categorized Answer.

Re: How do I match this without lookbehind?
by Shakya (Initiate) on Oct 03, 2007 at 18:15 UTC
    one more -
    my @results = $string =~ m!~([^~]+)!g;

    Originally posted as a Categorized Answer.