【问题标题】:Remove all content between two strings using regular expressions使用正则表达式删除两个字符串之间的所有内容
【发布时间】: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>

现在假设我想删除&lt;VisibleObject&gt;&lt;/VisibleObject&gt; 之间的所有内容只留下:

        <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>

上面有一些类似的线程和问题,但似乎没有一个对这个问题特别好(或根本没有)。

任何帮助将不胜感激。

【问题讨论】:

  • 查找(&lt;VisibleObject&gt;)[\S\s]*?(&lt;/VisibleObject&gt;),替换$1$2怎么样?
  • 借助 Notepad++,您可以使用 XSLT 转换,并轻松修改任何 XML。不确定 Sublime Text 是否支持使用 XSLT 修改 XML 文件。
  • 确实,正如@WiktorStribiżew 所提到的,这可以通过运行身份转换的XSLT 轻松处理,然后重写&lt;VisibleObject&gt; 元素。此外,强烈建议不要在X/HTML documents 上使用正则表达式。

标签: regex xml sublimetext3


【解决方案1】:

带有崇高窗口的图像:

您可以通过Find找到它,然后Replace并确保勾选最左侧的选项。

【讨论】:

    【解决方案2】:

    根据this page.,Sublime 似乎使用 PCRE

    这意味着您应该能够使用 PCRE 提供的很酷的技巧(主要是负前瞻)。这可以大大提高性能。

    我推荐的正则表达式是:

    <VisibleObject>(?:[^<]*(?!</VisibleObject).)+</VisibleObject>
    

    本质上,负前瞻确保无论何时出现&lt;(即在标签的开头),它都不是结束&lt;/VisibleObject&gt;

    . 是必需的,以便引擎可以在否定前瞻看到结束标记时回溯一个字符。

    您将需要使用替换 &lt;VisibleObject&gt;&lt;/VisibleObject&gt;

    【讨论】:

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