in reply to In need of a stupid regex trick

Something like

my @list=$str=~/("[^"]*"|\S+)/g

will do, but it doesnt handle escaping, and alas im on a box without perl installed so i havent tested it.


---
demerphq

    First they ignore you, then they laugh at you, then they fight you, then you win.
    -- Gandhi


Replies are listed 'Best First'.
Re: Re: In need of a stupid regex trick
by CountZero (Bishop) on Jan 04, 2004 at 21:50 UTC
    This seems to work but it still needs something extra besides a regex :(

    my $str='one "two three" four five "six seven eight" nine'; my @list=grep defined, $str=~/"([^"]*)"|(\S+)/g; print join "\n", @list;

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: Re: In need of a stupid regex trick
by CountZero (Bishop) on Jan 04, 2004 at 21:36 UTC
    Almost, but it keeps the " around the strings.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      my @list = grep defined, $str=~/"([^"]*)"|(\S+)/g;
        Great minds think alike (beat you by one minute)

        CountZero

        "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      Oops. (still untested...)

      my @list=$str=~/((?<=")[^"]*(?=")|\S+)/g

      ---
      demerphq

        First they ignore you, then they laugh at you, then they fight you, then you win.
        -- Gandhi


        That doesn't work at all; It gives following results:

        $VAR1 = 'one'; $VAR2 = '"two'; $VAR3 = 'three"'; $VAR4 = ' four five '; $VAR5 = '"six'; $VAR6 = 'seven'; $VAR7 = 'eight"'; $VAR8 = 'nine';

        CountZero

        "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law