Posted by: bgopi | August 22, 2012

Drools regular expression – matcher example

You can use “and” or “&&” / “or” or “||” but && and || are deprecated, from 5.2 onwards use and/or

This matches exception class like com.test.exception.BADException

<yourvariable> matches “^.*com.test.exception.BADException.*$”

This matches pattern some thing like “test-service-cashflow”, “test-service-business”, “test-service-credit”

<yourvariable> matches “^.*test-service.*.*$”

This matches exactly “MyError”

<yourvariable> == “MyError”

when
$msg: <YourClass>(<variable> == “MyError”) and <YourClass>(<yourvariable> matches “^.*test-service.*.*$”)
then
System.out.println(“Rule Executed “);
$msg.setSomething(“do something here if you want”);
end

when
$msg: <YourClass>(<variable> == “MyError”)  && <YourClass>(<yourvariable> matches “^.*test-service.*.*$”)
then
System.out.println(“Rule Executed “);
$msg.setSomething(“do something here if you want”);
end

This can be used for matching two sub string in the string i.e., it has BADException and USER_NOT_FOUND_ERROR

<YourClass>(<yourvariable> matches “(?s).*BADException.*USER_NOT_FOUND_ERROR.*”)

(?s) – is single line modifier, you can use (?i) – for ignoring case : matches “(?i).*BADException.*USER_NOT_FOUND_ERROR.*”)

We can use both: <YourClass>(<yourvariable> matches “(?i)(?s).*BADException.*USER_NOT_FOUND_ERROR.*”)

Go here for complete understanding of rules and expressions: http://docs.jboss.org/drools/release/5.2.0.Final/drools-expert-docs/html/ch05.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: