Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Regular Expression match number of lines

807580May 5 2010 — edited May 7 2010
Hi,

I need to validate with regular expression the maximum number of characters per line and the a maximum number of lines. I've managed to use a regex for max number of characters per line.
int maxRows = 3;
String strPattern = "^.{0,100}$";// 100 characters per line
Pattern pattern = Pattern.compile(strPattern, Pattern.MULTILINE);
Matcher matcher = pattern.matcher(value);
		
boolean found = false;
int index = 0;
while (matcher.find()) {
	System.out.println(String.format("I found the text \"%s\" starting at "
              + "index %d and ending at index %d.%n", matcher.group(), matcher.start(),matcher.end()));
	found = true;
	index ++;
}
		
return found && (index <= maxRows);
I'm interested to remove the while loop from code and change the pattern used to match maxRows.

Thanks,
Catalin
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 4 2010
Added on May 5 2010
3 comments
488 views