【问题标题】:Mule expression regex issue骡子表达式正则表达式问题
【发布时间】: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


【解决方案1】:

我找到了一个目前可行的解决方案,但我认为使用正则表达式必须有更好的解决方案:

<file:inbound-endpoint responseTimeout="10000" connector-ref="input" path="C:/test">
        <file:filename-regex-filter pattern="
                            0\.${environment}\.REPD\.(.*).GZ,
                            ${environment}(.*)PERSONNOTI(.*)\.xml\.gz,
                            ${environment}(.*)PERSONNOTI(.*)voucher\.xml" caseSensitive="false" />
    </file:inbound-endpoint>
    <choice>
        <when expression="#[(message.inboundProperties.originalFilename.toLowerCase()).startsWith('0.${environment}.REPD'.toLowerCase()) &amp;&amp;
                        (message.inboundProperties.originalFilename.toLowerCase()).endsWith('gz')]">
            <gzip-uncompress-transformer/>
            // Specific behaviour for this type of files 
        </when>
        <when expression="#[(message.inboundProperties.originalFilename.toLowerCase()).startsWith('${environment}'.toLowerCase()) &amp;&amp;
                        (message.inboundProperties.originalFilename.toLowerCase()).contains('personnoti') &amp;&amp;
                        (message.inboundProperties.originalFilename.toLowerCase()).endsWith('.xml.gz')]">
            // Specific behaviour for this type of files
        </when>
        <when expression="#[(message.inboundProperties.originalFilename.toLowerCase()).startsWith('${environment}'.toLowerCase()) &amp;&amp;
                        (message.inboundProperties.originalFilename.toLowerCase()).contains('personnoti') &amp;&amp;
                        (message.inboundProperties.originalFilename.toLowerCase()).endsWith('voucher.xml')]">
            // Specific behaviour for this type of files
        </when>
    </choice>

我在互联网上搜索了几个小时,但没有找到解决问题的有效方法。如果有人知道如何使用正则表达式或其他评估器来解决这个问题,我很好奇......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-25
    • 1970-01-01
    相关资源
    最近更新 更多