I have an XML file (call it module.xml) as below opened in an editor like Notepad++ or Geany:
<?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>
Now I want to replace string module/
with mycompany/module/
in only tags and attributes containing .phtml
filenames only, i.e. in content of the template
attribute and <template>
tag.
How can I do this with a regular expression? I am out of ideas when it comes to selective find/replace with regular expressions.
Note that this will work with properly formatted XML files.
You can use the regex (?=.*\.phtml)module\/
and replace with mycompany/module/
.
This:
(?=.*\.phtml)
Is a positive lookahead: it will ensure that the regular expression will be matched in the line. module\/
is the string you want to replace with an escape on the /
.
Also, you should read this well-known answer about parsing with regular expression.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments