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

my @nn=split /\d\)/, $l; doesnt seem to include the number or the ) What I think I want is "split the string at the character that precedes \d\)"

Replies are listed 'Best First'.
Re: splitting a string on a reqexp
by Corion (Patriarch) on Nov 29, 2010 at 10:17 UTC

    What error do you get?

    When dealing with syntax errors, (and also almost every other error), I find it most helpfull to eliminate things until the error goes away.

    Have you looked at the examples in the split documentation and compared them to your code?

Re: splitting a string on a reqexp
by johngg (Canon) on Nov 29, 2010 at 10:41 UTC

    Something like

    knoppix@Microknoppix:~$ perl -E ' > $str = q{abc1)def2)g(h)i3)jkl}; > say for split m{(?<=\d)\)}, $str;' abc1 def2 g(h)i3 jkl knoppix@Microknoppix:~$

    perhaps?

    Cheers,

    JohnGG

Re: splitting a string on a reqexp
by fisher (Priest) on Nov 29, 2010 at 10:18 UTC
    You mean something like
    $hh="1)foo2)bar3)blabla"; @nn=split /\d\)/, $hh; print join ':', @nn; print "\n";
    ?
Re: splitting a string on a reqexp
by moritz (Cardinal) on Nov 29, 2010 at 10:37 UTC

      It is possible to delimit regular expressions with other chars, and I though that perhaps the OP was stumbling over this feature of perl, and accidentally using round brackets as a delimiter, so I did a quick test

      split ( ), '48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21' split m( ), '48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21' split qr( ), '48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21'

      The second two expressions split the list, but without anything to tell perl that the first is a regular expressions it was not treated as one. I also tried:

      split (\s+), '48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21'

      But that resulted in a syntax error unrelated to split.

        FWIW split ( ), 'a b c' is parsed as split(), 'a b c '. So it's a call to split with no arguments. That call is part of a list, and the string literal is the last part of the list. Certainly not what the OP wants.

Re: splitting a string on a reqexp
by hbm (Hermit) on Nov 29, 2010 at 15:54 UTC

    To "split the string at the character that precedes \d\)", use a zero-width lookahead for the digit and paren:

    split /.(?=\d\))/, $1