The fact that you have in your test data an apostrophe that does not delimit a quotation makes things harder (the apostrophe in not if it's quoted). I don't think there's a way to do the Right Thing that kind of fuzzy data, but according to your written specification, here's a way to do it.

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:

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;
(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.

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:

my $float = qr/\d+\.\d+/; my $sentence = qr/ (?: $float | $singlequoted | $doublequoted | [^ +.] )+ \. /xm; $data .= "g = 9.8 m/s.";
You can add other exceptions similarly...

blokhead


In reply to Re: split $data, $unquoted_value; by blokhead
in thread split $data, $unquoted_value; by Ovid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.