Dev Dev - 11 months ago 44
Java Question

Regular expression not working in drools 5.3.0

I am trying to use regular expression in drool rule engine to find perticular keywords in sql queries(String). But even simplest regex are not working.



Drool syntax:-
when Query :Bean(Value matches "(\s|\r)(VALUES)(\|\r)")

What i am trying to do is, to find Values keyword in a query, where (\n or \r or \s or ')' or '(' ) may be present before or after it.

For example
insert into table values (xyz);

insert into table
values (xyz);

insert into table values

The code is running without error but it is not finding the keyword.
I am using drools 5.3.0 and java jdk 1.8.

I tried
also for \ in java. Still no luck.

Any guidance will be appreciated.

Answer Source

You should bear in mind that matches requires a full string match. So, to match a string with a whole word VALUES you need to make sure you match the chars before and after the word, and .* (with a DOTALL modifier) is the usual choice:



  • (?s) - a DOTALL inline modidier to make . match any char
  • .* - any 0+ chars, as many as possible
  • \\bVALUES\\b - a whole word VALUES (as \b are word boundaries)
  • .* - any 0+ chars, as many as possible.

Tto make sure there are specific chars around VALUES, you may use


or the same with character classes:


Remember that a \ should be escaped to define a literal \ and to match a backslash, you need to use 4 backslashes. So, ([\\s)])(VALUES)([\\\\\r]) matches a whitespace or ), then VALUES and \ or a CR symbol after the word.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download