Split by spaces but not when phrase is inside quotes...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • afrinspray@gmail.com

    #1

    Split by spaces but not when phrase is inside quotes...

    I'm writing a function that parses a nested list string that might look
    like this:
    ( "HELLO WORLD!" 1231231 awesome ( 1 2 ) )

    I wrote the logic already and it starts by splitting the string by the
    space character (or tab or newline).
    ex... $tklist = preg_split("/\s|\n|\t/", $str);

    This works except in the string case, where "HELLO WORLD!" should not
    be two parts. How do I split by spaces but keep phrases inside quotes
    in tact? Is there an easy way to do this?

    Thanks,
    Mike

  • Tony

    #2
    Re: Split by spaces but not when phrase is inside quotes...

    <afrinspray@gma il.com> wrote in message
    news:1116281172 .350907.44840@g 43g2000cwa.goog legroups.com...[color=blue]
    > I'm writing a function that parses a nested list string that might look
    > like this:
    > ( "HELLO WORLD!" 1231231 awesome ( 1 2 ) )
    >
    > I wrote the logic already and it starts by splitting the string by the
    > space character (or tab or newline).
    > ex... $tklist = preg_split("/\s|\n|\t/", $str);
    >
    > This works except in the string case, where "HELLO WORLD!" should not
    > be two parts. How do I split by spaces but keep phrases inside quotes
    > in tact? Is there an easy way to do this?[/color]

    Remove the substrings surrounded by quotes first. Then don't parse the
    phrases that were enclosed - only split the rest of the string.


    Comment

    • afrinspray

      #3
      Re: Split by spaces but not when phrase is inside quotes...

      Brilliant. Thanks.

      Mike

      Comment

      Working...