3 Replies Latest reply: May 7, 2010 3:03 AM by 807580 RSS

    Regular Expression match number of lines

    807580
      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
        • 1. Re: Regular Expression match number of lines
          YoungWinston
          croitoru wrote:
          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.
          I'm interested to remove the while loop from code and change the pattern used to match maxRows.
          From what I can see, your first regex is simply matching the first 100 characters of any line. Why not use:
          line.length() > 100 ? line.substring(0, 100) : line
          As far as the second one is concerned, I suspect "^(.*$){3}" will do the trick; however you'll probably have to set the start point yourself (or UncleAlice will show you whatever backtracker expression you need).

          To match any meaningful string pattern over a set of lines though, I suspect you're going to have to concatenate them yourself or set up some sort of LineWindow class.

          Winston
          • 2. Re: Regular Expression match number of lines
            807580
            return value.matches(".{0,100}(?:\n.{0,100}){0,2}");
            If you can't be sure the line separators will always be just '&#92;n', you can use
            ".{0,100}(?:(?:\r\n|[\r\n]).{0,100}){0,2}"
            instead.

            @YoungWinston: You're over-complicating it: the OP is validating, not extracting.
            • 3. Re: Regular Expression match number of lines
              807580
              @uncle_alice Thank you. It works fine.