Parsing strings and obeying quotes

Discussion in 'Perl Misc' started by Mia, Sep 12, 2006.

  1. Mia

    Mia Guest

    Hi All,

    I'm trying to parse a string into an array of words or phrases,
    something like:

    my $string = "abc def ghi";
    my @words = split(/ /,$string);

    ....but I also want to handle the case where there are quoted phrases
    within the string, i.e.:

    my $string = 'abc "def ghi"';
    or
    my $string = "abc 'def ghi'";

    I want to preserve the quoted phrase as one element. Such a simple
    thing, one would think, but I've not found a technique yet.

    Anyone have a hint for me?

    -Mia
     
    Mia, Sep 12, 2006
    #1
    1. Advertising

  2. Mia <> wrote in news:%CBNg.3347$AP2.33@fed1read10:

    > I'm trying to parse a string into an array of words or phrases,
    > something like:
    >
    > my $string = "abc def ghi";
    > my @words = split(/ /,$string);
    >
    > ...but I also want to handle the case where there are quoted phrases
    > within the string, i.e.:
    >
    > my $string = 'abc "def ghi"';
    > or
    > my $string = "abc 'def ghi'";
    >
    > I want to preserve the quoted phrase as one element. Such a simple
    > thing, one would think, but I've not found a technique yet.
    >
    > Anyone have a hint for me?


    You should read the Perl FAQ before posting your question:

    perldoc -q inside

    Sinan
     
    A. Sinan Unur, Sep 12, 2006
    #2
    1. Advertising

  3. Mia

    J. Gleixner Guest

    Mia wrote:
    > Hi All,
    >
    > I'm trying to parse a string into an array of words or phrases,
    > something like:
    >
    > my $string = "abc def ghi";
    > my @words = split(/ /,$string);
    >
    > ...but I also want to handle the case where there are quoted phrases
    > within the string, i.e.:
    >
    > my $string = 'abc "def ghi"';
    > or
    > my $string = "abc 'def ghi'";
    >
    > I want to preserve the quoted phrase as one element. Such a simple
    > thing, one would think, but I've not found a technique yet.
    >
    > Anyone have a hint for me?


    perldoc -q delimited
     
    J. Gleixner, Sep 12, 2006
    #3
  4. Mia

    Brian Wakem Guest

    A. Sinan Unur wrote:

    > Mia <> wrote in news:%CBNg.3347$AP2.33@fed1read10:
    >
    >> I'm trying to parse a string into an array of words or phrases,
    >> something like:
    >>
    >> my $string = "abc def ghi";
    >> my @words = split(/ /,$string);
    >>
    >> ...but I also want to handle the case where there are quoted phrases
    >> within the string, i.e.:
    >>
    >> my $string = 'abc "def ghi"';
    >> or
    >> my $string = "abc 'def ghi'";
    >>
    >> I want to preserve the quoted phrase as one element. Such a simple
    >> thing, one would think, but I've not found a technique yet.
    >>
    >> Anyone have a hint for me?

    >
    > You should read the Perl FAQ before posting your question:
    >
    > perldoc -q inside



    In addition to this FAQ entry, I have had success with Damian Conway's
    Text::Balanced for stuff similar to this.

    http://search.cpan.org/~dconway/Text-Balanced-1.98/lib/Text/Balanced.pm


    --
    Brian Wakem
    Email: http://homepage.ntlworld.com/b.wakem/myemail.png
     
    Brian Wakem, Sep 12, 2006
    #4
  5. Mia

    Dr.Ruud Guest

    Mia schreef:

    > I'm trying to parse a string into an array of words or phrases,
    > something like:
    >
    > my $string = "abc def ghi";
    > my @words = split(/ /,$string);
    >
    > ...but I also want to handle the case where there are quoted phrases
    > within the string, i.e.:
    >
    > my $string = 'abc "def ghi"';
    > or
    > my $string = "abc 'def ghi'";
    >
    > I want to preserve the quoted phrase as one element. Such a simple
    > thing, one would think, but I've not found a technique yet.
    >
    > Anyone have a hint for me?


    Think up complexer strings.

    "Dungit," he said, "I left my "o'lang syne" at the baker's."

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Sep 12, 2006
    #5
    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. GIMME
    Replies:
    2
    Views:
    935
    GIMME
    Feb 11, 2004
  2. Chris White

    Quotes/Double Quotes in Image Control

    Chris White, Sep 22, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    4,916
    Hermit Dave
    Sep 22, 2004
  3. Chris
    Replies:
    1
    Views:
    13,763
    Oisin
    Mar 24, 2006
  4. Richard Bondi

    XPathAPI not obeying XPath properly

    Richard Bondi, Aug 25, 2004, in forum: Java
    Replies:
    2
    Views:
    681
    Richard Bondi
    Aug 27, 2004
  5. sheldonlg

    Obeying only part of a style

    sheldonlg, Feb 28, 2008, in forum: HTML
    Replies:
    3
    Views:
    357
    Bergamot
    Feb 28, 2008
Loading...

Share This Page