Uploaded image for project: 'JBRULES'
  1. JBRULES
  2. JBRULES-2745

Problems in the use of escapes in the "matches" operator

This issue belongs to an archived project. You can view it, but you can't modify it. Learn more

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • Major
    • 5.2.0.M1
    • 5.1.1.FINAL
    • None
    • None
    • Hide

      Use the java code from Drools Project provided by JBossTools or JBDS.
      The rule:

      //this regex search for 2 letters + 3 numbers + 2 letters, like: "ab123cd"
      rule "Regex-A"
      when
      $message: Message(message matches "\w\w\d

      {3}\w\w")
      then
      System.out.println( "Regex-A working" );
      end

      //this regex search for 2 letters + 3 numbers + 2 letters, like: "ab123cd"
      rule "Regex-B"
      when
      $message: Message(message matches "\\w\\w
      d{3}

      \\w
      w")
      then
      System.out.println( "Regex-B working" );
      end

      //this regex search for 2 letters + 3 numbers + 2 letters, like: "ab123cd"
      rule "Regex-C"
      when
      $message: Message(message.toString matches "\w\w\d

      {3}

      \w\w")
      then
      System.out.println( "Regex-C working" );
      end

      Only Regex-C and Regex-B works.

      Show
      Use the java code from Drools Project provided by JBossTools or JBDS. The rule: //this regex search for 2 letters + 3 numbers + 2 letters, like: "ab123cd" rule "Regex-A" when $message: Message(message matches "\w\w\d {3}\w\w") then System.out.println( "Regex-A working" ); end //this regex search for 2 letters + 3 numbers + 2 letters, like: "ab123cd" rule "Regex-B" when $message: Message(message matches "\\w\\w d{3} \\w w") then System.out.println( "Regex-B working" ); end //this regex search for 2 letters + 3 numbers + 2 letters, like: "ab123cd" rule "Regex-C" when $message: Message(message.toString matches "\w\w\d {3} \w\w") then System.out.println( "Regex-C working" ); end Only Regex-C and Regex-B works.
    • Hide

      Use escapes like:
      Cheese( type matches "(Buffalo)?
      S*Mozarella" )

      or toString
      Cheese( type.toString matches "(Buffalo)?
      S*Mozarella" )

      Show
      Use escapes like: Cheese( type matches "(Buffalo)? S*Mozarella" ) or toString Cheese( type.toString matches "(Buffalo)? S*Mozarella" )

    Description

      According to official documentation, it is not necessary to use escapes:
      "In contrast to Java, escapes are not needed within regular expressions written as string literals." (a note in topic 4.8.2.1.5.)

      But it is not true. Actually even the example of the documentation does not work if you do not use escape:

      //type is a String
      Cheese( type matches "(Buffalo)?\S*Mozarella" )

      ... should be changed to:

      //type is a String
      Cheese( type matches "(Buffalo)?
      S*Mozarella" )

      Curiously, if you have a attribute of the attribute which matches a regex, you shouldn't use escapes:

      //type is "complex type" and value is a String
      Cheese( type.value matches "(Buffalo)?\S*Mozarella" )

      ... it's work!

      That said, a workaround to work with simple String attributes, you should use "toString":

      //type is "String"
      Cheese( type.toString matches "(Buffalo)?\S*Mozarella")

      Attachments

        Activity

          People

            rhn-support-tsurdilo Tihomir Surdilovic (Inactive)
            rhn-support-alazarot Alessandro Lazarotti
            Archiver:
            rhn-support-ceverson Clark Everson

            Dates

              Created:
              Updated:
              Resolved:
              Archived:

              PagerDuty