【问题标题】:How can I remove a line with Ant replaceregexp, but not leave an empty line如何使用 Ant replaceregexp 删除一行,但不留下空行
【发布时间】:2012-11-20 21:07:04
【问题描述】:

我有以下文本文件:

清单版本:3.0.0 Ant 版本:Apache Ant 1.7.1 创建者:10.0-b19(Sun Microsystems Inc.) 要求捆绑:org.eclipse.linuxtools.cdt.libhover;bundle-version="1. 1.0" Bundle-SymbolicName:com.qnx.doc.gestures.lib_ref 捆绑版本:3.0.0.20121120 捆绑本地化:插件 捆绑包名称:%plugin.name 捆绑供应商:%plugin.providername

我正在尝试在 replaceregexp 任务中使用以下模式

regexp pattern='Require-Bundle: org.eclipse.linuxtools.cdt.libhover;bundle-version="1. [\s\S]*'

以这样的方式结束:

清单版本:3.0.0 Ant 版本:Apache Ant 1.7.1 创建者:10.0-b19(Sun Microsystems Inc.) 1.0" Bundle-SymbolicName:com.qnx.doc.gestures.lib_ref 捆绑版本:3.0.0.20121120 捆绑本地化:插件 捆绑包名称:%plugin.name 捆绑供应商:%plugin.providername

问题是我不断得到这个:

清单版本:3.0.0 Ant 版本:Apache Ant 1.7.1 创建者:10.0-b19(Sun Microsystems Inc.) 1.0" Bundle-SymbolicName:com.qnx.doc.gestures.lib_ref 捆绑版本:3.0.0.20121120 捆绑本地化:插件 捆绑包名称:%plugin.name 捆绑供应商:%plugin.providername

我的正则表达式应该是什么来摆脱空行?

谢谢。

【问题讨论】:

  • 这会给您留下无效的清单 - 1.0" 是前一行的延续,因为清单规范需要包装长行。理想情况下,您应该使用适当的清单文件解析器,但不幸的是 Ant manifest 任务似乎不支持 removing 属性,只能添加或修改它们。
  • 谢谢。我意识到清单将是无效的,并且一旦我知道如何摆脱空行,我也计划删除 1.0" :-) 我只是不需要对该捆绑包的依赖。

标签: ant


【解决方案1】:

这样的事情应该可以工作:

Require-Bundle: org\.eclipse\.linuxtools\.cdt\.libhover;bundle-version="1\.\s*1.0"\s*

(使用\s* 匹配零个或多个空白字符,包括\r\n)但是由于您正在处理清单文件,因此使用适当的清单解析器会更有意义。不幸的是,Ant <manifest> 任务没有提供删除属性的方法,但使用 <script> 任务非常简单:

<property name="manifest.file" location="path/to/manifest.txt" />

<script language="javascript"><![CDATA[
    importPackage(java.io);
    importPackage(java.util.jar);

    // read the manifest
    manifestFile = new File(project.getProperty('manifest.file'));
    manifest = new Manifest();
    is = new FileInputStream(manifestFile);
    manifest.read(is);
    is.close();

    // remove the offending attribute
    manifest.getMainAttributes().remove(new Attributes.Name('Require-Bundle'));

    // write back to the original file
    os = new FileOutputStream(manifestFile);
    manifest.write(os);
    os.close();
]]></script>

【讨论】:

  • 非常感谢 Ian,感谢您对我的技术的快速响应和更正。你解决了我的问题,我学到了一些新东西。
【解决方案2】:
<replaceregexp file="manifest.mf" match='Require-Bundle: org.eclipse.linuxtools.cdt.libhover;bundle-version=\"[^\"]+\"[\r\n]*' replace="" flags="m"/>

这对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    • 2016-10-23
    • 2021-11-11
    相关资源
    最近更新 更多