The trick is to think like a lexer. A quotation should be treated as one atomic entity, just like a character. Now the components that make up a sentence are entire quotations and individual non-period characters. Breaking it down this way, it's straightforward to see:
(delimited quote regexes stolen shamelessly from Abigail-II's Re: regex regexen) The only thing is to be careful that a quotation match is attempted first in the $sentence definition.my $data = qq[ this is some text. A period (".") usually terminates a statement. But not if it is quoted. Regardless of whether or not single quotes, '.', are used. And yes, "Mr. Ovid," even lines with a period in the middle of a quo +te. ]; my $doublequoted = qr/"[^"\\]*(?:\\.[^"\\]*)*"/m; my $singlequoted = qr/'[^'\\]*(?:\\.[^'\\]*)*'/m; my $sentence = qr/ (?: $singlequoted | $doublequoted | [^.] )* \. +/xm; my @items = $data =~ /($sentence)/g; print "[$_]\n" for @items;
This solution does not require the period(s) inside a quotation to be at the end of the quotation, which is a problem I think some of the other solutions suffer from.
Update: to allow for non-terminator "." characters inside floating point numbers (as per Re^2: split $data, $unquoted_value;), here is a rough addition:
You can add other exceptions similarly...my $float = qr/\d+\.\d+/; my $sentence = qr/ (?: $float | $singlequoted | $doublequoted | [^ +.] )+ \. /xm; $data .= "g = 9.8 m/s.";
blokhead
In reply to Re: split $data, $unquoted_value;
by blokhead
in thread split $data, $unquoted_value;
by Ovid
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |