我有兴趣使用正则表达式解析Drools规则文件.有一个包含整个.drl文件内容的字符串,我想有4个子字符串:
<name><attribute><conditional element><action>根据官方文档,Drools规则具有以下结构:
rule "<name>"
<attribute>*
when
<conditional element>*
then
<action>*
end
Run Code Online (Sandbox Code Playgroud)
我尝试过使用这种模式,但效果不佳:
^rule"(.|\n|\r|\t)+"(.|\n|\r|\t)+\bwhen\b(.|\n|\r|\t)+\bthen\b(.|\n|\r|\t)+\bend\b?$
Run Code Online (Sandbox Code Playgroud)
有没有人知道我该怎么办?
我知道你的问题是关于regexp,但我强烈建议不要使用它.有太多的情况会因你的正则表达式而失败...例如,单个单词的规则名称不需要"",规则关键字不需要是行中的第一个东西,等等......
/*this is a comment on the start of the line*/ rule X...
Run Code Online (Sandbox Code Playgroud)
而不是正则表达式,只需直接使用DrlParser,它将为您提供所需的所有信息:
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 );
Run Code Online (Sandbox Code Playgroud)
PackageDescr.getRules()将为您提供文件中的所有RuleDescr,每个RuleDescr都有一个getName()来为您提供规则名称等.所有类型安全,没有边缘情况等.
| 归档时间: |
|
| 查看次数: |
1972 次 |
| 最近记录: |