如何使用Ant replaceregexp删除一行,但不留下空行
我有以下文本文件: Manifest-Version: 3.0.0 Ant-Version: Apache Ant 1.7.1 Created-By: 10.0-b19 (Sun Microsystems Inc.) Require-Bundle: org.eclipse.linuxtools.cdt.libhover;bundle-version="1. 1.0" Bundle-SymbolicName: com.qnx.doc.gestures.lib_ref Bundle-Version: 3.0.0.20121120 Bundle-Localization: plugin Bundle-Name: %plugin.name Bundle-Vendor: %plugin.providername 清单版本:3.0.0 Ant版本:ApacheAnt 1.7.1 创建人:10.0-b19(太阳微系统公司) 需要捆绑包:org.eclipse.linuxtools.cdt.libhover;bundle version=“1。 1.0" Bundle SymbolicName:com.qnx.doc.signatures.lib\u ref 捆绑版本:3.0.0.20121120 捆绑本地化:插件 捆绑包名称:%plugin.Name 捆绑供应商:%plugin.providername 我试图在replaceregexp任务中使用以下模式 regexp pattern='Require-Bundle: org.eclipse.linuxtools.cdt.libhover;bundle-version="1. [\s\S]*' regexp pattern='Require-Bundle:org.eclipse.linuxtools.cdt.libhover;bundle version=“1。 [\s\s]*' 要结束此操作,请执行以下操作: Manifest-Version: 3.0.0 Ant-Version: Apache Ant 1.7.1 Created-By: 10.0-b19 (Sun Microsystems Inc.) 1.0" Bundle-SymbolicName: com.qnx.doc.gestures.lib_ref Bundle-Version: 3.0.0.20121120 Bundle-Localization: plugin Bundle-Name: %plugin.name Bundle-Vendor: %plugin.providername 清单版本:3.0.0 Ant版本:ApacheAnt 1.7.1 创建人:10.0-b19(太阳微系统公司) 1.0" Bundle SymbolicName:com.qnx.doc.signatures.lib\u ref 捆绑版本:3.0.0.20121120 捆绑本地化:插件 捆绑包名称:%plugin.Name 捆绑供应商:%plugin.providername 问题是我一直都在想: Manifest-Version: 3.0.0 Ant-Version: Apache Ant 1.7.1 Created-By: 10.0-b19 (Sun Microsystems Inc.) 1.0" Bundle-SymbolicName: com.qnx.doc.gestures.lib_ref Bundle-Version: 3.0.0.20121120 Bundle-Localization: plugin Bundle-Name: %plugin.name Bundle-Vendor: %plugin.providername 清单版本:3.0.0 Ant版本:ApacheAnt 1.7.1 创建人:10.0-b19(太阳微系统公司) 1.0" Bundle SymbolicName:com.qnx.doc.signatures.lib\u ref 捆绑版本:3.0.0.20121120 捆绑本地化:插件 捆绑包名称:%plugin.Name 捆绑供应商:%plugin.providername 我的正则表达式应该是什么来摆脱空行如何使用Ant replaceregexp删除一行,但不留下空行,ant,Ant,我有以下文本文件: Manifest-Version: 3.0.0 Ant-Version: Apache Ant 1.7.1 Created-By: 10.0-b19 (Sun Microsystems Inc.) Require-Bundle: org.eclipse.linuxtools.cdt.libhover;bundle-version="1. 1.0" Bundle-SymbolicName: com.qnx.doc.gestures.li
谢谢。像这样的方法应该可以:
Require-Bundle: org\.eclipse\.linuxtools\.cdt\.libhover;bundle-version="1\.\s*1.0"\s*
(使用\s*
匹配零个或多个空白字符,其中包括\r
和\n
)但是,由于您处理的是清单文件,因此使用适当的清单解析器更有意义。不幸的是,Ant
任务没有提供删除属性的方法,但它对于
任务非常简单:
<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>
这对我来说很有用。这将给您留下一个无效的清单-
1.0“
是前一行的延续,因为清单规范要求包装长行。理想情况下,您应该使用适当的清单文件解析器,但不幸的是,Antmanifest
任务似乎不支持删除属性,只支持添加或修改属性。谢谢。我意识到清单将是无效的,我计划在找到如何消除空行的方法后也删除1.0“:-)我不需要依赖该捆绑包。非常感谢伊恩,感谢你对我技术的快速响应和纠正。你解决了我的问题,我学到了一些新东西。
<replaceregexp file="manifest.mf" match='Require-Bundle: org.eclipse.linuxtools.cdt.libhover;bundle-version=\"[^\"]+\"[\r\n]*' replace="" flags="m"/>