Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

How do I match this without lookbehind?

by tedv (Pilgrim)
on Dec 08, 2000 at 05:48 UTC ( [id://45668]=perlquestion: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://45668]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-03-28 21:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found