Regex 用正则表达式搜索并替换XML文件中的某些标记和属性
我在中打开了一个XML文件(称为module.XML),如下所示 编辑,如:Regex 用正则表达式搜索并替换XML文件中的某些标记和属性,regex,xml,search,replace,geany,Regex,Xml,Search,Replace,Geany,我在中打开了一个XML文件(称为module.XML),如下所示 编辑,如: skin_jsmodule/js/jquery.js skin_jsmodule/js/module-slider.js skin_cssmodule/module.css skin_jsmodule/js/module-video.js skin_jsmodule/js/nicEdit-latest.js 模块名称 模块 模块名称 真的 99 模块名称 模块 模块名称 真的 99 模块/create.phtml
skin_jsmodule/js/jquery.js
skin_jsmodule/js/module-slider.js
skin_cssmodule/module.css
skin_jsmodule/js/module-video.js
skin_jsmodule/js/nicEdit-latest.js
模块名称
模块
模块名称
真的
99
模块名称
模块
模块名称
真的
99
模块/create.phtml
现在,我想将字符串module/
替换为mycompany/module/
中仅包含.phtml
文件名的标记和属性,即模板
属性和
标记的内容
如何使用正则表达式实现这一点?我不知道如何使用正则表达式选择性地查找/替换。请注意,这将适用于格式正确的XML文件
您可以使用regex
(?=.\.phtml)模块\/
,并替换为mycompany/module/
这:
是一个积极的前瞻:它将确保正则表达式在行中匹配<代码>模块\/是要在/
上用转义替换的字符串
另外,您应该阅读这篇文章。为什么不使用xml解析器呢?这个文件在编辑器中是可选的,我已经安装了ubuntu 14.04,所以除非您建议我使用sed/awk,否则我不能在编辑器中使用xml解析器(正如我在查询中已经说明的那样)我不知道是否有适用于Geany/Notepad++之类的软件?是的,这在当前xml中可以很好地工作,只要xml没有中断且格式良好,您肯定这会工作,对吗?然后请添加这个作为答案,并对使用的正则表达式进行一些解释。好的,谢谢您的答案和额外的信息,即使是关于XML解析的。接受它。
<?xml version="1.0"?>
<layout version="0.1.0">
<default>
<reference name="head">
<action method="addItem"><type>skin_js</type><name>module/js/jquery.js</name></action>
<action method="addItem"><type>skin_js</type><name>module/js/module-slider.js</name></action>
<action method="addItem"><type>skin_css</type><name>module/module.css</name></action>
<action method="addItem"><type>skin_js</type><name>module/js/module-video.js</name></action>
<action method="addItem"><type>skin_js</type><name>module/js/nicEdit-latest.js</name></action>
</reference>
<reference name="left">
<block type="module/subscribe" name="module.left.subscribe" template="module/left-sidebar.phtml" />
</reference>
<reference name="right">
<block type="module/subscribe" name="module.right.subscribe" template="module/right-sidebar.phtml" before="-" />
</reference>
<reference name="top.links">
<action method="addLink" translate="label title" module="module" ifconfig="module/settings/quicklinks">
<label>Module Title</label>
<url>module</url>
<title>Module Title</title>
<prepare>true</prepare>
<urlParams/>
<position>99</position>
</action>
</reference>
<reference name="footer_links">
<action method="addLink" translate="label title" module="module" ifconfig="module/settings/quicklinks">
<label>Module Title</label>
<url>module</url>
<title>Module Title</title>
<prepare>true</prepare>
<urlParams/>
<position>99</position>
</action>
</reference>
</default>
<module_index_index>
<reference name="content">
<block type="module/list" name="module_list" template="module/list.phtml" />
</reference>
</module_index_index>
<module_create_index>
<reference name="content">
<block type="module/list" name="module.create">
<action method="setTemplate"><template>module/create.phtml</template></action>
</block>
</reference>
</module_create_index>
</layout>
(?=.*\.phtml)