在文本之前和之后替换

时间:2014-11-05 03:47:41

标签: regex replace notepad++

我需要替换,但在字符之间保持不变:

示例:

<column name="propertys_uid">30</column>
<column name="property_name">Villa</column>
<column name="property_street">5th street</column>
<column name="property_town">New York</column>

应该成为

<yourreference>30</yourreference>
<name>Villa</name>
<street>5th street</street>
<town>New York</town>

正如您所看到的,> ... <之间的内容不应更改。正常的查找和替换将不起作用 </column>始终需要与众不同。我认为我找到了使用<column name="propertys_uid">.*?</column>的第一步,但它不能用<yourreference>*?</yourreference> –

替换

非常感谢任何建议

3 个答案:

答案 0 :(得分:0)

您可以在替换中使用捕获组。所以,例如:

查找:<column name="propertys_uid">([^<]+)</column>

替换:<yourreference>$1</yourreference>

这会在您的示例中提供<yourreference>30</yourreference>


如果原始标签的名称在所需的替代品中播放,您也可以这样做:

查找:<column name="property_([^"]+)">([^<]+)</column>

替换:<$1>$2</$1>

这会在您的示例中提供<name>Villa</name>

答案 1 :(得分:0)

您可以录制宏并逐步完成每个替换。

开始录制您的宏,然后:

  • CTRL H
  • 选择&#39;正则表达式&#39; Search Mode

为每个元素重复以下步骤

  • Find What字段中添加您的正则表达式,例如<column name="propertys_uid">(.*?)</column>
  • 使用Replace With
  • <yourreference>$1</yourreference>字段中添加替换
  • 点击全部替换按钮

对每个元素完成后,停止录制宏并保存。

要在Windows 7/8上找到宏,请转到C:\Users\{{USERNAME}}\AppData\Roaming\Notepad++\shortcuts.xml。您必须关闭notepad ++才能将宏保存到xml文件中。

这样做有点令人费解,但会起作用并节省脑力!唷

答案 2 :(得分:0)

我在那里看到了notepad ++标签,你可以在notepad ++中使用group regex。

只需将您的正则表达式修改为<column name="propertys_uid">(.*)<\/column>,然后替换为<yourreference>\1</yourreference>,请确保您的搜索模式位于“正则表达式”中。

您将从<yourreference>30</yourreference>

获得类似<column name="propertys_uid">30</column>的内容