Regex ANT:如果发现某些多行模式,则使用loadfile设置属性

Regex ANT:如果发现某些多行模式,则使用loadfile设置属性,regex,ant,Regex,Ant,我有一个源代码文件,其中包括以下行: public boolean isBetaVersion() { return true; } 我尝试在ANT中读取此文件,并根据上述方法是否返回true或其他内容设置属性: <loadfile srcfile="path/to/MyFile.java" property="isBetaBuild"> <filterchain> <striplinebreaks/>

我有一个源代码文件,其中包括以下行:

   public boolean isBetaVersion() {
      return true;
   }
我尝试在ANT中读取此文件,并根据上述方法是否返回
true
或其他内容设置属性:

<loadfile srcfile="path/to/MyFile.java" property="isBetaBuild">
   <filterchain>
      <striplinebreaks/>
      <linecontainsregexp>
          <regexp pattern=".*\sisBetaVersion()\s\{\sreturn\strue.*"/>
      </linecontainsregexp>
   </filterchain>
</loadfile>


但它从来没有匹配(不幸的是,我是一个完全的regex noob)。有人知道会出什么问题吗?提前感谢。

您需要对括号进行转义,并允许多个空格字符。然后,
\b
锚可以用来匹配单词的开头和结尾,这在这里也很有用。我建议你试试这个:

<regexp pattern="\bisBetaVersion\(\)\s+\{\s+return\s+true\b"/>

您不需要在正则表达式周围使用
*
,因为您只需要查找源代码中是否包含该子字符串