Question about Regular expression

J

Jay

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!
 
D

David Squire

Jay said:
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.
 
X

Xicheng Jia

Jay said:
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
 
M

Mumia W.

Jay said:
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__
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top