(updated node) Actually, ignore me and just read what Zaxo and %mick have to say.

Hmmmm, this will get all non-capitalized words at the beginning of each sentance except the very first sentance and store them into an array:

my $s = 'hello world! how are you? whoops, forgot.'; my @no_caps = $s =~ /[.?!]\s*((?![A-Z])[a-z]\w+)/g;
Better to replace them on the spot says me:
$s =~ s/^((?![A-Z])[a-z]\w+)/ucfirst$1/e; $s =~ s/([.?!]\s*)((?![A-Z])[a-z]\w+)/$1.ucfirst$2/eg;
The first regex gets the first word of the string, the second takes care of the rest. Putting this back into your original code we get:
use strict; @ARGV = '/Perl/LearningPerl/Test'; while(<>){ if (/^((?![A-Z])[a-z]\w+)/) { print "$1 is not capitalized\n"; } while (/([.?!]\s*)((?![A-Z])[a-z]\w+)/g) { print "$2 is not capitalized\n"; } }
And that's ugly. The first if catches the first word of the file, and the while loop takes care of the rest.

And it is still broken, as newlines are the monkeywrench in this machine. Taking Zaxo's suggestion of slurping the entire file into a scalar will fix that (the error, not the ugliness):

my $file = do {local $/; <>}; if ($file =~ /^((?![A-Z])[a-z]\w+)/) { print "$1 is not capitalized\n"; } while ($file =~ /([.?!]\s*)((?![A-Z])[a-z]\w+)/g) { print "$2 is not capitalized\n"; }
Sorry for being too quick to respond.

jeffa

I shoulda waited for merlyn ...

In reply to (jeffa) Re: Match non-capitalized words at the beginning of each sentence (was: Regular Expressions) by jeffa
in thread Match non-capitalized words at the beginning of each sentence by WarrenBullockIII

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.