in reply to Re: Not quite a simple split
in thread Not quite a simple split

Ah, the master speaks. When I saw your post filled with (? syntax, I knew you had addressed the subtleties of the problem.

So, let me understand... the first thing, (?=\S) will fail if the next character is whitespace or there is no next character. I wonder why we need that? Ah, it interacts with the /g to say "no match at this postion" to actually skip the spaces! And the spaces naturally don't wind up in the returned array. Beautiful.

I also like the way the non-quote stuff is always first, and the quoted part is an optional part that follows, rather than having two totally different cases.

So first it matches everything that's not whitespace or a quote.

Then it picks up the quote, stuff inside it, and close quote. Then it has [^\s"]* again, and the whole thing is in a repeat star. That means it will handle anything with an even number of quotes in it, not just a single pair and end on the close-quote.

That is an interesting generalization, and I rather like it.

I suppose you couldn't pull the non-quote non-whitespace part out of the loop because it must be performed at least once. Ah, but you know it's not a space already, and taking out the 3rd line and changing the 7th line from * to + would work, and further allow things that begin with a quote. Would it not?

—John

Replies are listed 'Best First'.
Re: Re: Re: Not quite a simple split
by japhy (Canon) on Feb 02, 2004 at 14:15 UTC
    The regex could be changed to:
    @parts = $string =~ m{ ( (?: [^\s"]+ # one or more non-quote/whitespace | " [^"]* " # a quoted part )+ # one or more times ) }xg;
    This probably runs better.
    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;
      Why don't you need the (?=\S) in this version? Because it doesn't match an empty string (in the first one, either part was optional which made both parts optional at the same time)?
        Absolutely correct (on both accounts).
        _____________________________________________________
        Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
        s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;
      Your code counts 8"foo"8 as one token, but I think the token is supposed to terminate after the 2nd quote, leaving the second 8 as a separate token.
      m/([^\s"]+|[^\s"]*(?:"[^"]*"))/g

      The PerlMonk tr/// Advocate