2016-10-06 40 views
2

我有一個XML文件中的XML文件屬性(稱之爲module.xml),如下像記事本編輯器中打開++/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> 

現在我想替換字符串module/mycompany/module/僅包含僅包含.phtml文件名的標籤和屬性,即在template屬性和<template>標籤的內容中。

我該如何用正則表達式來做到這一點?當談到正則表達式中的選擇性查找/替換時,我沒有任何想法。

+0

你爲什麼不使用XML解析器? –

+0

這個文件在編輯器中運行,我安裝了ubuntu 14.04,所以除非你建議我使用sed/awk,否則我不能在編輯器中使用xml解析器(因爲我已經在查詢中說過了),因爲我不知道是否存在可用於Geany/Notepad ++ –

+0

類似[this](https://regex101.com/r/6MS8ry/1)? –

回答

1

請注意,這將與適當的格式化XML文件。


您可以使用正則表達式(?=.*\.phtml)module\/mycompany/module/取代。

此:

(?=.*\.phtml) 

是一個積極前瞻:這將確保正則表達式將在該行進行匹配。 module\/是您想用/上的轉義替換的字符串。


此外,你應該閱讀本well known answer about parsing with regex.

+0

好的,謝謝你的回答和額外的信息,甚至在XML解析。接受它。 –

相關問題