Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Question about Regular expression

Reply
Thread Tools

Question about Regular expression

 
 
Jay
Guest
Posts: n/a
 
      05-30-2006
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!

 
Reply With Quote
 
 
 
 
David Squire
Guest
Posts: n/a
 
      05-30-2006
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.
 
Reply With Quote
 
 
 
 
Xicheng Jia
Guest
Posts: n/a
 
      05-30-2006
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:umper -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

 
Reply With Quote
 
Mumia W.
Guest
Posts: n/a
 
      05-30-2006
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:arsewords, 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__

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seek xpath expression where an attribute name is a regular expression GIMME XML 3 12-29-2008 03:11 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Matching abitrary expression in a regular expression =?iso-8859-1?B?bW9vcJk=?= Java 8 12-02-2005 12:51 AM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments