4.10. Limit the Number of Lines in Text
Problem
You need to check whether a string is composed of five or fewer lines, without regard for how many total characters appear in the string.
Solution
The exact characters or character sequences used as line
separators can vary depending on your operating system’s convention,
application or user preferences, and so on. Crafting an ideal solution
therefore raises questions about what conventions for indicating the
start of a new line should be supported. The following solutions support
the standard MS-DOS/Windows (‹\r\n›), legacy Mac OS (‹\r›), and Unix/Linux/BSD/OS X (‹\n›) line break
conventions.
Regular expression
The following three flavor-specific regexes contain two
differences. The first regex uses atomic groups, written as ‹(?>⋯)›, instead of noncapturing groups,
written as ‹(?:⋯)›, because they have
the potential to provide a minor efficiency improvement here for the
regex flavors that support them. Python and JavaScript do not support
atomic groups, so they are not used with those flavors. The other
difference is the tokens used to assert position at the beginning and
end of the string (‹\A›
or ‹^› for the beginning
of the string, and ‹\z›,
‹\Z›, or ‹$› for the end). The reasons for
this variation are discussed in depth later in this recipe. All three
flavor-specific regexes, however, match exactly the same
strings:
\A(?>[^\r\n]*(?>\r\n?|\n)){0,4}[^\r\n]*\z| Regex options: None |
| Regex flavors: .NET, Java, PCRE, Perl, Ruby |
\A(?:[^\r\n]*(?:\r\n?|\n)){0,4}[^\r\n]*\Z ...