Search and replace in only certain tags and attributes in XML file by a regular expression

Vicky Dev

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.

Thomas Ayoub

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.

edited at
0

Comments

0 comments
Login to comment

Related

regular expression to search in xml file

Regular expression to replace text between XML tags using Notepad++

MongoDB search by tags with regular expression

Javascript regular expression to strip HTML tags with certain attributes but keep the inner text

PhpStorm regular expression search and replace it

PHP Regular expression: try to remove attributes style"..." but only those inside <p> and <span> tags

Powershell - Search 2 values into .xml file with regular expression

How to find tags with only certain attributes - BeautifulSoup

Regular expression find and replace to wrap tags

Exclude regular expression in file search

Search for a regular expression in a file in Ansible

Search And Replace Text But Exclude Certain Tags

Regular expression search everything before a certain separator

Regular expression return only certain values PHP

Regular expression search replace in Sublime Text 2

Regular expression for search and replace in Microsoft Word

Eclipse IDE, regular expression search and replace

Dreamweaver help needed with regular expression search and replace

Transform XML with XSLT but only certain nodes, attributes,

Regular expression replace only one matching

regular expression to replace &, but only inside a link

How to write this regular expression to only replace url

python regular expression replace only in parentheses

Javascript Regular Expression to replace only outside of a match

How to get two or more matching tags in a xml file using regular expression

Parse XML response only if certain tags are available

JS - Regular expression to replace all instance of string (with variable attributes)

PyCharm. Regular expression to replace all script src attributes

find with regular expression to replace file content

TOP Ranking

HotTag

Archive