I'm interested in parsing a Drools rule file using regular expressions. Having a string with the content of the whole .drl file, I'd like to have 4 substrings:
<name><attribute><conditional element><action>A Drools rule has the following structure, according to the official documentation:
rule "<name>"
<attribute>*
when
<conditional element>*
then
<action>*
end
I've tried using this pattern, but it hasn't worked well:
^rule"(.|\n|\r|\t)+"(.|\n|\r|\t)+\bwhen\b(.|\n|\r|\t)+\bthen\b(.|\n|\r|\t)+\bend\b?$
Does anyone have an idea of how could I proceed?
I know your question is about regexp, but I would strongly advise against using it. There are way too many cases that will fail with your regexp... for instance, rule names that are a single word don't need "", rule keyword does not need to be the first thing in the line, etc...
/*this is a comment on the start of the line*/ rule X...
Instead of regexp, just use the DrlParser directly and it will give you all the information you need:
String drl = "package foo \n"
+ "declare Bean1 \n"
+ "field1: java.math.BigDecimal \n"
+ "end \n"
+ "rule bigdecimal\n"
+ "when \n"
+ "Bean1( field1 == 0B ) \n"
+ "then \n"
+ "end";
DrlParser parser = new DrlParser(LanguageLevelOption.DRL6);
PackageDescr pkgDescr = parser.parse( null, drl );
PackageDescr.getRules() will give you all the RuleDescr in the file, each RuleDescr has a getName() to give you the rule name, etc. All type safe, no edge cases, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With