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

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

    Details

    • Type: Bug
    • Status: Resolved (View Workflow)
    • Priority: Major
    • Resolution: Done
    • Affects Version/s: 5.1.1.FINAL
    • Fix Version/s: 5.2.0.M1
    • Component/s: None
    • Labels:
      None
    • Steps to Reproduce:
      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.
    • Workaround Description:
      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")

        Gliffy Diagrams

          Attachments

            Issue Links

              Activity

                People

                • Assignee:
                  tsurdilovic Tihomir Surdilovic
                  Reporter:
                  alessandrolt Alessandro Lazarotti
                • Votes:
                  0 Vote for this issue
                  Watchers:
                  1 Start watching this issue

                  Dates

                  • Created:
                    Updated:
                    Resolved: