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

Regex Pattern.matches() Failes to match \b boundry Pattern.compile(REGEX) works

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      Linux x64 based. tested with Java 11 and freshly downloaded jdk-18


      A DESCRIPTION OF THE PROBLEM :
      Regex Pattern.matches(REGEX, STRING) Failes to match \b boundry
      Pattern.compile(REGEX).matcher(STRING) works as expected


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      just using \b at the end of a regex from Pattern.matches() function.

          if (Pattern.matches("HI\\b", "HI hi")) {
            System.out.println("I will not match properly");
          }
          if (Pattern.matches("HI\\b", "HI")) {
            System.out.println("I will match");
          }
          if (Pattern.compile("HI\\b").matcher("HI hi").find()) {
            System.out.println("I will match from compiled");
          }

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I will not match properly
      I will match
      I will match from compiled
      ACTUAL -
      I will match
      I will match from compiled

      ---------- BEGIN SOURCE ----------
          if (Pattern.matches("HI\\b", "HI hi")) {
            System.out.println("I will not match properly");
          }
          if (Pattern.matches("HI\\b", "HI")) {
            System.out.println("I will match");
          }
          if (Pattern.compile("HI\\b").matcher("HI hi").find()) {
            System.out.println("I will match from compiled");
          }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      use patther.compile pattern.

      FREQUENCY : always


            igraves Ian Graves
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: