4.10. Limit the Number of Lines in Text
Problem
You need to check whether a string is comprised 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 should be supported
to indicate the start of a new line. The following solutions support
the standard MS-DOS/Windows (‹\r\n›), legacy Mac OS (‹\r›), and Unix/Linux/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 match exactly the same strings:
\A(?>(?>\r\n?|\n)?[^\r\n]*){0,5}\z| Regex options: None |
| Regex flavors: .NET, Java, PCRE, Perl, Ruby |
\A(?:(?:\r\n?|\n)?[^\r\n]*){0,5}\Z| Regex ... |