Java Regex to limit the number of lines in text

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

The regex for matching 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

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)

Above regular expression validate that content has minimum zero lines and maximum 3 lines. Let’s verify the solution regex.

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());

Output : true

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());

Output : true

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());

Output : false

I will advise to play with 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.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode