简体   繁体   English

用正则表达式替换 Notepad++ 中标签内的文本

[英]Regular Expression to replace text inside tags in Notepad++

Hivemind!蜂群思维!

I am trying to edit Memsource *.mxliff files with Notepad++.我正在尝试使用 Notepad++ 编辑 Memsource *.mxliff 文件。
When I create a search task, both source and target lines mix up in a huge list.当我创建一个搜索任务时,源行和目标行混合在一个巨大的列表中。
But I need to make amendments only for lines inside <target>.../<target> tags.但我只需要对<target>.../<target>标签内的行进行修改。

For example:例如:

1. <target>от 15 до 35 °C</target>
2. <target>Допустимый диапазон температур воздуха от -40 °C до +70 °C {1&gt;1)&lt;1}</target>  

Inside these lines I need to replace all instances of degrees with a non-breaking space version:在这些行中,我需要用不间断的空格版本替换所有度数实例:

Find: (\\d) °C求: (\\d) °C
Replace with: $1 $2替换为: $1 $2

What is the most optimal way to do so?这样做的最佳方法是什么?
Any hints would be much appreciated.任何提示将不胜感激。 Thanks a lot!非常感谢!

  • Ctrl + H Ctrl + H
  • Find what: (?:<target>|\\G)(?:(?!target)[\\s\\S])*?\\K(\\d+)\\s*(°C)(?=.*</target>)求什么: (?:<target>|\\G)(?:(?!target)[\\s\\S])*?\\K(\\d+)\\s*(°C)(?=.*</target>)
  • Replace with: $1&nbsp;$2替换为: $1&nbsp;$2
  • CHECK Match case检查火柴盒
  • CHECK Wrap around检查环绕
  • CHECK Regular expression检查正则表达式
  • UNCHECK . matches newline取消选中. matches newline . matches newline
  • Replace all全部替换

Explanation:解释:

(?:                         # non capture group
    <target>                # opening tag
  |                         # OR
    \G                      # restart from last match position
)                           # end group
(?:(?!target)[\s\S])*?      # 0 or more any character but not "target"
\K                          # forget all we have seen until this position
(\d+)                       # group 1, 1 or more digits
\s*                         # 0 or more spaces
(°C)                        # group 2
(?=.*</target>)             # must be followed by </target>

Replacement:替代品:

$1              # content of group 1 (i.e. the temperature)
&nbsp;          # non breaking space
$2              # content of group 2 (i.e. °C)

Screen capture (before):屏幕截图(之前):

在此处输入图片说明

Screen capture (after):屏幕截图(之后):

在此处输入图片说明

Assuming that we'd have one °C in a target tag, maybe some expression similar to:假设我们在目标标签中有一个°C ,可能有一些类似于:

(<\s*target\s*>[\s\S]*?\d)\s+(°C[\s\S]*?<\s*\/target\s*>)

and replaced with,并替换为,

$1$2

might be OK to look into.可能可以研究一下。

RegEx Demo正则表达式演示


If you wish to simplify/update/explore the expression, it's been explained on the top right panel of regex101.com .如果你想简化/更新/探索表达式,它已在regex101.com 的右上角面板中进行了解释 You can watch the matching steps or modify them in this debugger link , if you'd be interested.如果您有兴趣,可以在此调试器链接中观看匹配步骤或修改它们。 The debugger demonstrates that how a RegEx engine might step by step consume some sample input strings and would perform the matching process.调试器演示了 RegEx 引擎如何逐步使用一些示例输入字符串并执行匹配过程。


Short alternative as always ....一如既往的简短替代....
Find what: (\\d+) (?=°C)找出什么: (\\d+) (?=°C)
Replace all with: $1NBSP全部替换为: $1NBSP
Done!完毕!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM