0

According to the Drools Documentation, I should be able to match the following regex in a rule, since the matches operator

"Matches a field against any valid Java Regular Expression."

However,

$firstName : String(value matches "[^A-Za-z]") from $person.name.firstName

results in an error when attempting to evaluate the rule. What am I missing here?

Edit - originally had "contains" instead of "matches" in the expression. This was a type-o made when re-typing an expression similar to the one I'm working with.

Syscall
  • 19,327
  • 10
  • 37
  • 52
josh-cain
  • 4,997
  • 7
  • 35
  • 55

2 Answers2

3

The link contains the text The Operator matches Matches a field against any valid Java Regular Expression.

$firstName : String(this matches ".*[^A-Za-z].*") from $person.name.firstName

Note that the word is "matches" and not "find"!

Edit

And you cannot use "value" with String, there's no such method.

laune
  • 31,114
  • 3
  • 29
  • 42
  • As JAndy has correctly pointed out, it may be simpler to use this with a "Person" pattern. – laune Jul 18 '14 at 14:43
  • This regex fixed it, and I did have to change the object like Andy suggested - since String doesn't have a "value" field the above is not correct. Didn't pick up on that distinction between matches and find, I just assumed that matches did the same thing as something like String's .find method. – josh-cain Jul 18 '14 at 14:54
  • The `value` escaped me. But you can use - see modified answer. – laune Jul 18 '14 at 15:01
1

What you need is matches operator and you should probably write your drl like

when:
  Person( firstName matches "[^A-Za-z]" )
then:
  // ..
end

The doc for contains says

The operator contains is used to check whether a field that is a Collection or array contains the specified value.

So it cannot be used for a String

kaskelotti
  • 4,709
  • 9
  • 45
  • 72