【发布时间】:2016-08-15 17:53:36
【问题描述】:
我正在尝试在 sublime 3 中使用正则表达式,以删除 两个字符串之间的所有内容,一个 XML 文件。
假设这是我的内容:
<Body name="ground">
<mass>0</mass>
<mass_center> 0 0 0</mass_center>
<inertia_xx>0</inertia_xx>
<inertia_yy>0</inertia_yy>
<inertia_zz>0</inertia_zz>
<inertia_xy>0</inertia_xy>
<inertia_xz>0</inertia_xz>
<inertia_yz>0</inertia_yz>
<!--Joint that connects this body with the parent body.-->
<Joint />
<VisibleObject>
<!--Set of geometry files and associated attributes, allow .vtp, .stl, .obj-->
<GeometrySet>
<objects />
<groups />
</GeometrySet>
<!--Three scale factors for display purposes: scaleX scaleY scaleZ-->
<scale_factors> 1 1 1</scale_factors>
<!--transform relative to owner specified as 3 rotations (rad) followed by 3 translations rX rY rZ tx ty tz-->
<transform> -0 0 -0 0 0 0</transform>
<!--Whether to show a coordinate frame-->
<show_axes>false</show_axes>
<!--Display Pref. 0:Hide 1:Wire 3:Flat 4:Shaded Can be overriden for individual geometries-->
<display_preference>4</display_preference>
</VisibleObject>
<WrapObjectSet>
<objects />
<groups />
</WrapObjectSet>
</Body>
现在假设我想删除<VisibleObject> 和</VisibleObject> 之间的所有内容只留下:
<Body name="ground">
<mass>0</mass>
<mass_center> 0 0 0</mass_center>
<inertia_xx>0</inertia_xx>
<inertia_yy>0</inertia_yy>
<inertia_zz>0</inertia_zz>
<inertia_xy>0</inertia_xy>
<inertia_xz>0</inertia_xz>
<inertia_yz>0</inertia_yz>
<!--Joint that connects this body with the parent body.-->
<Joint />
<VisibleObject>
</VisibleObject>
<WrapObjectSet>
<objects />
<groups />
</WrapObjectSet>
</Body>
上面有一些类似的线程和问题,但似乎没有一个对这个问题特别好(或根本没有)。
任何帮助将不胜感激。
【问题讨论】:
-
查找
(<VisibleObject>)[\S\s]*?(</VisibleObject>),替换$1$2怎么样? -
借助 Notepad++,您可以使用 XSLT 转换,并轻松修改任何 XML。不确定 Sublime Text 是否支持使用 XSLT 修改 XML 文件。
-
确实,正如@WiktorStribiżew 所提到的,这可以通过运行身份转换的XSLT 轻松处理,然后重写
<VisibleObject>元素。此外,强烈建议不要在X/HTML documents 上使用正则表达式。
标签: regex xml sublimetext3