Split quoted text

Discussion in 'Ruby' started by Michael Weller, Jul 14, 2004.

  1. Hi!
    Can anybody help me with following, it seems like I can find a solution
    that's shorter than 50LOC and I thought there must be a smarter way to
    do it...
    My input would be something like "\this is\" \"my text\"" and I want to
    have an array containing ["\"this is\"", "\"my text\""].
    I know it can't be that hard, but ...

    Thanks for any reply!

    Michael
     
    Michael Weller, Jul 14, 2004
    #1
    1. Advertising

  2. something like this?
    b=[]; t.scan(/(\".*?\")(?:\s*)/) { b << $1 }; p b
    i'm sure there's something better though...
    Alex

    On Wed, Jul 14, 2004 at 09:27:03PM +0900, Michael Weller wrote:
    > Hi!
    > Can anybody help me with following, it seems like I can find a solution
    > that's shorter than 50LOC and I thought there must be a smarter way to
    > do it...
    > My input would be something like "\this is\" \"my text\"" and I want to
    > have an array containing ["\"this is\"", "\"my text\""].
    > I know it can't be that hard, but ...
    >
    > Thanks for any reply!
    >
    > Michael
    >
    >


    mvg,
    Alex

    --
    When women love us, they forgive us everything, even our crimes; when they do
    not love us, they give us credit for nothing, not even our virtues.
    -- Honor'e de Balzac
     
    Alexander Kellett, Jul 14, 2004
    #2
    1. Advertising

  3. Hi --

    On Wed, 14 Jul 2004, Michael Weller wrote:

    > Hi!
    > Can anybody help me with following, it seems like I can find a solution
    > that's shorter than 50LOC and I thought there must be a smarter way to
    > do it...
    > My input would be something like "\this is\" \"my text\"" and I want to
    > have an array containing ["\"this is\"", "\"my text\""].
    > I know it can't be that hard, but ...


    p '"this is" "my text"'.scan(/"[^"]+"/)
    # => ["\"this is\"", "\"my text\""]


    David

    --
    David A. Black
     
    David A. Black, Jul 14, 2004
    #3
  4. David A. Black wrote:

    >Hi --
    >
    >On Wed, 14 Jul 2004, Michael Weller wrote:
    >
    >
    >
    >>My input would be something like "\this is\" \"my text\"" and I want to
    >>have an array containing ["\"this is\"", "\"my text\""].
    >>I know it can't be that hard, but ...
    >>
    >>

    >
    >p '"this is" "my text"'.scan(/"[^"]+"/)
    ># => ["\"this is\"", "\"my text\""]
    >
    >
    >David
    >
    >
    >

    Yes, I think that's exactly what I searched... Thanks a lot!

    Michael
     
    Michael Weller, Jul 14, 2004
    #4
  5. Hi,

    Michael Weller wrote:
    > Can anybody help me with following, it seems like I can find a solution
    > that's shorter than 50LOC and I thought there must be a smarter way to
    > do it...
    > My input would be something like "\this is\" \"my text\"" and I want to
    > have an array containing ["\"this is\"", "\"my text\""].
    > I know it can't be that hard, but ...


    Is quoting " needed?

    0% ruby -rcsv -e 'p CSV.parse_line(%["this is" "my text"], ?\s)'
    ["this is", "my text"]

    0% ruby -rcsv -e 'p CSV.parse(%["this is" "my text"|foo bar], ?\s, ?|)'
    [["this is", "my text"], ["foo", "bar"]]

    Regards,
    // NaHi
     
    NAKAMURA, Hiroshi, Jul 14, 2004
    #5
  6. On Jul 14, 2004, at 5:51 AM, Michael Weller wrote:

    > David A. Black wrote:
    >
    >> Hi --
    >>
    >> On Wed, 14 Jul 2004, Michael Weller wrote:
    >>
    >>
    >>> My input would be something like "\this is\" \"my text\"" and I want
    >>> to have an array containing ["\"this is\"", "\"my text\""].
    >>> I know it can't be that hard, but ...
    >>>

    >>
    >> p '"this is" "my text"'.scan(/"[^"]+"/)
    >> # => ["\"this is\"", "\"my text\""]

    On that note, if you have escaped quotes you can use the following:
    p '"he said \"hello\"" "field"'.scan(/"(?:\\.|[^"])*?"/)
    # => ["\"he said \\\"hello\\\"\"", "\"field\""]
    >>
    >>
    >> David
    >>
    >>

    > Yes, I think that's exactly what I searched... Thanks a lot!
    >
    > Michael
    >
     
    Charles Mills, Jul 14, 2004
    #6
  7. "NAKAMURA, Hiroshi" <> schrieb im Newsbeitrag
    news:...
    > Hi,
    >
    > Michael Weller wrote:
    > > Can anybody help me with following, it seems like I can find a

    solution
    > > that's shorter than 50LOC and I thought there must be a smarter way to
    > > do it...
    > > My input would be something like "\this is\" \"my text\"" and I want

    to
    > > have an array containing ["\"this is\"", "\"my text\""].
    > > I know it can't be that hard, but ...

    >
    > Is quoting " needed?
    >
    > 0% ruby -rcsv -e 'p CSV.parse_line(%["this is" "my text"], ?\s)'
    > ["this is", "my text"]
    >
    > 0% ruby -rcsv -e 'p CSV.parse(%["this is" "my text"|foo bar], ?\s, ?|)'
    > [["this is", "my text"], ["foo", "bar"]]


    Very nice! I usually use something like this for similar cases:

    rx = %r{
    (?:"(?:[^\\"]|\\.)*")
    | (?:'(?:[^\\']|\\.)*')
    | \S+
    }x

    str.scan rx

    Regards

    robert
     
    Robert Klemme, Jul 14, 2004
    #7
  8. Michael Weller

    George Ogata Guest

    Charles Mills <> writes:

    > On Jul 14, 2004, at 5:51 AM, Michael Weller wrote:
    >
    >> David A. Black wrote:
    >>
    >>> Hi --
    >>>
    >>> On Wed, 14 Jul 2004, Michael Weller wrote:
    >>>
    >>>
    >>>> My input would be something like "\this is\" \"my text\"" and I want
    >>>> to have an array containing ["\"this is\"", "\"my text\""].
    >>>> I know it can't be that hard, but ...
    >>>>
    >>>
    >>> p '"this is" "my text"'.scan(/"[^"]+"/)
    >>> # => ["\"this is\"", "\"my text\""]

    > On that note, if you have escaped quotes you can use the following:
    > p '"he said \"hello\"" "field"'.scan(/"(?:\\.|[^"])*?"/)
    > # => ["\"he said \\\"hello\\\"\"", "\"field\""]


    There's also shellwords:

    irb(main):001:0> require 'shellwords'
    => true
    irb(main):002:0> Shellwords.shellwords('"he said \\"hello\\"" "field"')
    => ["he said \"hello\"", "field"]
     
    George Ogata, Jul 15, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. flarosa
    Replies:
    6
    Views:
    12,019
    flarosa
    Apr 11, 2006
  2. Frank Olieu
    Replies:
    5
    Views:
    2,172
    Vinayak
    Nov 28, 2005
  3. oliver
    Replies:
    5
    Views:
    420
    Paul McGuire
    Mar 11, 2005
  4. Replies:
    2
    Views:
    113
    Perusion hostmaster
    Jul 11, 2003
  5. Robert Oschler
    Replies:
    2
    Views:
    126
    peterS.
    Aug 2, 2005
Loading...

Share This Page