How do I build a regular expression that will match sentences where each word starts with a capital letter?

You need a regular expression that will match text where every word begins with a capital letter. For example, you regular expression will match:

    Every Good Boy Deserves Fruit

But will not match

   Hi, my name is Bill



    use strict;
    use warnings;

    # Get input
    while (<>) {
        # Provide a way of exiting
        last if ($_ eq "\n");
        print "   line: $_";
        print "matched: $_" if ($_ =~ m/^([A-Z&]\S*\s*)+$/);

The regular expression is explained as follows:

    ^ From the start of the text
    [A-Z&] Match a capital letter or an &
    \S* Followed by zero or more non-whitespace characters
    \s* Followed by zero or more whitespace characters
    + Do all the above one or more times 
    $ until the end of the text

This will match:

    Cats And Dogs
    A Man Walks Down The Street
    Johnson & Johnson CauLife Ins. Co.
    Should, See This-line

But will not match:

    Ignore this line?
    johnson & johnson CaulLife Ins. Co.
    A man walks down the street

See also

For more information on regular expressions, see:

    perldoc perlretut
    perldoc perlre
Revision: 1.5 [Top]