Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8183391

Regex: End of line found more than once for non-multiline regex

    XMLWordPrintable

Details

    Description

      FULL PRODUCT VERSION :
      java version "1.8.0_131"
      Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
      Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      macOS Sierra 10.12.5

      A DESCRIPTION OF THE PROBLEM :
      Given a pattern "$" that does not specify Pattern.MULTILINE flag, I would expect it to match only once, no matter the test string. It should match only at the end of the full string, including newlines.

      But matching against the simple string "\n" matches twice.



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Execute following code:

          public static void main(String[] args) {
            Pattern p = Pattern.compile("$");
            Matcher m = p.matcher("\n");
            int count = 0;
            while (m.find()) {
                count++;
            }
            System.out.println(String.format("Matched %d times.", count));
          }



      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I would expect it to print "Matched 1 times".
      ACTUAL -
      It prints "Matched 2 times".

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------

          public static void main(String[] args) {
            Pattern p = Pattern.compile("$");
            Matcher m = p.matcher("\n");
            int count = 0;
            while (m.find()) {
                count++;
            }
            System.out.println(String.format("Matched %d times.", count));
          }

      ---------- END SOURCE ----------

      Attachments

        Issue Links

          Activity

            People

              rgiulietti Raffaello Giulietti
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: