B
Brian McGonigle
Actually, all you need is the last two expressions. Here it is revised:
#!/usr/bin/perl
$text = 'The "quick" brown "fox jumped ""over"" the" lazy dog.';
while ($text =~ /"(.*?)"/g) {
if ($text =~ /".*?(""$1"").*?"/) {
push @matches, $1;
print "REGEX 1: $1 \n";
}
else {
push @matches, $1;
print "REGEX 2: $1\n";
}
}
print "MATCHES: @matches\n";
It prints...
REGEX 2: quick
REGEX 2: fox jumped
REGEX 1: ""over""
REGEX 2: the
MATCHES: quick fox jumped ""over"" the
#!/usr/bin/perl
$text = 'The "quick" brown "fox jumped ""over"" the" lazy dog.';
while ($text =~ /"(.*?)"/g) {
if ($text =~ /".*?(""$1"").*?"/) {
push @matches, $1;
print "REGEX 1: $1 \n";
}
else {
push @matches, $1;
print "REGEX 2: $1\n";
}
}
print "MATCHES: @matches\n";
It prints...
REGEX 2: quick
REGEX 2: fox jumped
REGEX 1: ""over""
REGEX 2: the
MATCHES: quick fox jumped ""over"" the
Brian said:Got it!!!
$text = 'The "quick" brown "fox jumped ""over"" the" lazy dog.';
while ($text =~ /"(.*?)"/g) {
if ($text =~ /"$_("".*?"")/) {
push @matches, ($1);
print "FOUND: $1\n";
}
elsif ($text =~ /(""$1"")(.*?)"/) {
push @matches, $1;
print "FOUND: $1 \n";
}
else {
push @matches, $1;
print "FOUND: $1\n";
}
}
print "MATCHES: @matches\n";
Prints...
FOUND: quick
FOUND: fox jumped
FOUND: ""over""
FOUND: the
MATCHES: quick fox jumped ""over"" the
mortb said:...and
(?<=")(""|[^"])+(?=")
gets me...
(1) quick
(2) brown (notice the space before)
(3) fox jumped ""over"" the
this got rid of the quotes but introduced the " brown" error
Perhaps you can live with the quotes....
/mortb
"(""|[^"])+"
gets me:
(1) "quick"
(2) "fox jumped ""over"" the"
- the | is an OR-operator which means that after " you match either "" or
any character except "
I've tried to get rid of the initial and ending quotes -- I think it's
possible in the same expression -- but I haven't succeeded -- yet.
tricky? -- yes!
/mortb