[rules-users] capturing matches from regular expressions?

Godmar Back godmar at gmail.com
Sat Sep 22 11:00:58 EDT 2007


On 9/22/07, Edson Tirelli <tirelli at post.com> wrote:
>
>    Sorry, nope. Is there a use case for that?
>

For instance, I'd like to write a rule where I need to be alerted of
all facts where a specific field is of the form "(.*)Suffix".  The
action I'm then taking requires the "(.*)" as a parameter.
The value of "Suffix" does not matter to the action and should be
restricted to the .drl file (as it matters there only.)

If I cannot capture the match, I will have to pay both a cost in
runtime and in expressiveness. In runtime because the regular
expression match is repeated in the action.
In expressiveness, because my code is now less localized since there
will be 2 places that deal with "Suffix".  If I repeated "Suffix" in
the match and action as a string literal, changing it would require
changing it in two places:

when  n : Name (value matches "(.*)Suffix")
then   TakeAction(n, "Suffix")

I do not know if I could use a variable, as in

final String suffix = "Suffix";
when  n : Name (value matches ("(.*)" + suffix))
then   TakeAction(n, suffix)

but even then, the code is less expressive and the match would need to
repeated; in any event, I'd like to be able to write something along
the lines of:

when  n : Name (value matches "(.*)Suffix")   .... some way to bind
"presuffix" to $1 ....
then   TakeAction(presuffix)

where "presuffix" captures $1 in the match. This way, I'd have to
change "Suffix" only once.

 - Godmar



More information about the rules-users mailing list