Skip to Content
Regular Expressions Cookbook
book

Regular Expressions Cookbook

by Jan Goyvaerts, Steven Levithan
May 2009
Intermediate to advanced
510 pages
15h
English
O'Reilly Media, Inc.
Content preview from Regular Expressions Cookbook

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Regular Expressions Cookbook, 2nd Edition

Regular Expressions Cookbook, 2nd Edition

Jan Goyvaerts, Steven Levithan

Publisher Resources

ISBN: 9780596802837Catalog PageErrata