【发布时间】:2015-08-01 18:02:04
【问题描述】:
我有一个 ftp 入站端点,其中存储了很多文件(不是全部),所以我添加了一个文件名正则表达式过滤器,其中包含我想从 FTP 目录中获取的不同正则表达式:
<file:filename-regex-filter pattern="
${environment}\.TEST.xml,
0\.${environment}\.REPD\.(.*).GZ,
${environment}(.*)PERSONNOTI(.*)\.xml\.gz,
${environment}(.*)PERSONNOTI(.*)voucher\.xml" caseSensitive="false" />
这很好用,但现在我想根据文件类型执行特定的行为。 因此我使用了一个带有不同 when 表达式的选择组件:
我的第一个想法是使用一个选择组件,当表达式使用与 filename-regex-filter 中使用的相同正则表达式并且在那里工作时:
<when expression="#[message.inboundProperties.originalFilename.matches('0\.${environment}\.REPD\.(.*).GZ')]">
// Specific behaviour for this type of files
</when>
这产生了以下错误消息:
Exception stack is:
1. [Error: illegal escape sequence: .]
[Near : {... age.inboundProperties.originalFilename.matches('0\.T\.REPD\.(.*).GZ') ....}]
^
[Line: 1, Column: 55] (org.mule.api.expression.InvalidExpressionException)
org.mule.el.mvel.MVELExpressionLanguage:238(http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/expression/InvalidExpressionException.html)
2. [Error: illegal escape sequence: .]
[Near : {... age.inboundProperties.originalFilename.matches('0\.T\.REPD(.*).GZ') ....}]
这意味着表达式属性不需要转义字符,所以我决定不转义它们
<when expression="#[message.inboundProperties['originalFilename'].matches('${environment}.TEST.xml')]">
//Specific behavior for this type of files...
</when>
<when expression="#[message.inboundProperties.originalFilename.matches('0.${environment}.REPD.*.GZ')]">
//Specific behavior for this type of files...
</when>
<otherwise>
<file:outbound-endpoint path="C:/test/result/other" responseTimeout="10000" outputPattern="#[message.inboundProperties.originalFilename]"/>
</otherwise>
这里的消息总是被路由到 else 子句,除了第一个没有使用通配符的表达式...
使用 when 表达式是一种好习惯吗?放置表达式的正确方法是什么?
【问题讨论】:
-
您的输入与您的正则表达式混合,因此如果您在输入中包含这些正则表达式特殊字符,它们最终会破坏您的正则表达式的语法。因此,您要么需要将它们正确地放入现有的正则表达式中,要么将它们全部转义:
*到\*、.到\.、[到\[、`\ ` 到 `\\` 和很快。 Which characters need escaping.。也许您的语言已经像其他一些语言一样具有Regex.Escape()方法。然后在你的输入序列上使用它。 -
你能把它从评论移到问题吗? (包括您用于转义的代码。)这将使您的问题在您迄今为止的研究中更加完整,并且更容易被了解该领域的人回答。
标签: regex expression mule