Question about Regular expression

Discussion in 'Perl Misc' started by Jay, May 30, 2006.

  1. Jay

    Jay Guest

    I am going to write a function that the search engine done.
    in search engine, we may using double quotation to specify a pharse
    like "I love you",
    How can I using regular expression to sperate each pharse?

    test case:
    "I love" all "of you"

    I would like it return:
    "I love", all, "of you"

    Thank you!
    Jay, May 30, 2006
    #1
    1. Advertising

  2. Jay

    David Squire Guest

    Jay wrote:
    > I am going to write a function that the search engine done.
    > in search engine, we may using double quotation to specify a pharse
    > like "I love you",
    > How can I using regular expression to sperate each pharse?
    >
    > test case:
    > "I love" all "of you"
    >
    > I would like it return:
    > "I love", all, "of you"
    >


    Here's a solution that meets your spec., but it is ugly and a stretch,
    and I am not saying that I would do it this way :) Over to the golfers...

    ----

    #!/usr/bin/perl
    use strict;
    use warnings;

    while (<DATA>) {
    print "Original: $_";
    s/(.?)("[^"]*")(.?)/{if ($1 && $3) {",$1$2,$3"} elsif ($3) {"$2,$3"}
    elsif ($1) {",$1$2"} else {$2}}/ge;
    print "Result: $_";
    }


    __DATA__
    "I love" all "of you"
    "I hate" almost "none of" you
    I wonder "how this" will work "when" one "tests" a longer "example like
    this?"
    one
    "two"

    ----

    Output:

    Original: "I love" all "of you"
    Result: "I love", all, "of you"
    Original: "I hate" almost "none of" you
    Result: "I hate", almost, "none of", you
    Original: I wonder "how this" will work "when" one "tests" a longer
    "example like this?"
    Result: I wonder, "how this", will work, "when", one, "tests", a longer,
    "example like this?"
    Original: one
    Result: one
    Original: "two"
    Result: "two"


    DS

    PS. Leading or trailing whitespace will bite you.
    David Squire, May 30, 2006
    #2
    1. Advertising

  3. Jay

    Xicheng Jia Guest

    Jay wrote:
    > I am going to write a function that the search engine done.
    > in search engine, we may using double quotation to specify a pharse
    > like "I love you",
    > How can I using regular expression to sperate each pharse?
    >
    > test case:
    > "I love" all "of you"
    >
    > I would like it return:
    > "I love", all, "of you"


    I assumed you dont treat escaped double-quotes \" differently in your
    phases..

    perl -MData::Dumper -wle '
    $str=q("I love" all about "of you" );
    @x = ($str =~ /"[^"]*?"|\S+/g);
    print Dumper \@x'

    $VAR1 = [
    '"I love"',
    'all',
    'about',
    '"of you"'
    ];

    you need to find a way to remove double-quotes by yourself :).

    Xicheng
    Xicheng Jia, May 30, 2006
    #3
  4. Jay

    Mumia W. Guest

    Jay wrote:
    > I am going to write a function that the search engine done.
    > in search engine, we may using double quotation to specify a pharse
    > like "I love you",
    > How can I using regular expression to sperate each pharse?
    >
    > test case:
    > "I love" all "of you"
    >
    > I would like it return:
    > "I love", all, "of you"
    >
    > Thank you!
    >


    As Dave Weaver said, you can install Text::parsewords, but you could
    also use Text::Balanced, which is installed with Perl by default:

    use strict;
    use warnings;
    use Text::Balanced qw(extract_quotelike extract_multiple);

    my $str = q{"I love" all "of you"};
    my (@parts);

    @parts = grep !/^\s+$/, extract_multiple ($str,
    [ qr(\s*),
    \&extract_quotelike,
    ]);

    print join(', ',@parts), "\n";

    __END__
    Mumia W., May 30, 2006
    #4
    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. Andrew Munn

    Regular expression question...

    Andrew Munn, Jun 29, 2003, in forum: Perl
    Replies:
    1
    Views:
    2,117
    rakesh sharma
    Jun 30, 2003
  2. Glenn Kidd

    Regular expression question

    Glenn Kidd, Aug 18, 2003, in forum: Perl
    Replies:
    0
    Views:
    927
    Glenn Kidd
    Aug 18, 2003
  3. VSK
    Replies:
    2
    Views:
    2,281
  4. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    839
    Alan Moore
    Dec 2, 2005
  5. GIMME
    Replies:
    3
    Views:
    11,942
    vforvikash
    Dec 29, 2008
Loading...

Share This Page