Regex to Limit the Number of Lines in Text

In this Java regex tutorial, we will learn to test whether several lines in input text are between some minimum and maximum limit, without regard for how many total characters appear in the string.

The regex for matching the number of lines will depend on the exact characters or character sequences used as line separators. In practice, line separators can vary depending on your operating system’s convention, application or user preferences, and so on. Writing an ideal solution therefore depends on what conventions for indicating the start of a new line should be supported.

The following solution discussed in this tutorial supports the standard MS-DOS/Windows (“\r\n”), legacy Mac OS (“\r”), and Unix/Linux/BSD/OS X (“\n”) line break conventions.

Regex : \\A(?>[^\r\n]*(?>\r\n?|\n)){0,3}[^\r\n]*\\z

1. Explanation of Regex

\A          # Assert position at the beginning of the string.
(?>         # Group but don't capture or keep backtracking positions:
  [^\r\n]*  #   Match zero or more characters except CR and LF.
  (?>       #   Group but don't capture or keep backtracking positions:
    \r\n?   #     Match a CR, with an optional following LF (CRLF).
   |        #    Or:
    \n      #     Match a standalone LF character.
  )         #   End the noncapturing, atomic group.
){0,4}      # End group; repeat between zero and four times.
[^\r\n]*    # Match zero or more characters except CR and LF.
\z          # Assert position at the end of the string.


CR : Carriage Return (\r\n)
LF : Line Feed (\n)

The above regular expression validates that content has a minimum of zero lines and a maximum of 3 lines. Let’s verify the solution regex.

2. Demo

2.1. Verify with zero lines

StringBuilder builder = new StringBuilder();
        
String regex = "\\A(?>[^\r\n]*(?>\r\n?|\n)){0,3}[^\r\n]*\\z";
 
Pattern pattern = Pattern.compile(regex);
 
Matcher matcher = pattern.matcher(builder.toString());
System.out.println(matcher.matches());

The program output:

true

2.2. Verify with two lines

StringBuilder builder = new StringBuilder();
builder.append("Test Line 1");
builder.append("\n");
builder.append("Test Line 2");
builder.append("\n");
        
String regex = "\\A(?>[^\r\n]*(?>\r\n?|\n)){0,3}[^\r\n]*\\z";
 
Pattern pattern = Pattern.compile(regex);
 
Matcher matcher = pattern.matcher(builder.toString());
System.out.println(matcher.matches());

The program output:

true

2.3. Verify with six lines

StringBuilder builder = new StringBuilder();
builder.append("Test Line 1");
builder.append("\n");
builder.append("Test Line 2");
builder.append("\n");
builder.append("Test Line 3");
builder.append("\n");
builder.append("Test Line 4");
builder.append("\n");
builder.append("Test Line 5");
builder.append("\n");
builder.append("Test Line 6");
builder.append("\n");
        
String regex = "\\A(?>[^\r\n]*(?>\r\n?|\n)){0,3}[^\r\n]*\\z";
 
Pattern pattern = Pattern.compile(regex);
 
Matcher matcher = pattern.matcher(builder.toString());
System.out.println(matcher.matches());

The program output:

false

I will advise you to play with the above simple regular expression to try more variation.

Happy Learning !!

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.