如何使用正则表达式查找单词并将其复制到特定位置?

How can I use regular expressions to find a word and copy it to a specific location?

你能告诉我如何使用正则表达式找到标签之间写的内容,复制并粘贴到标签之间吗? 绞尽脑汁一个小时,找不到公式))

示例:

<category id="1"><name>Мужские куртки</name><short></short></category>
<category id="2"><name>Мужские брюки</name><short></short></category>
<category id="3"><name>Мужские портянки</name><short></short></category>

需要:

<category id="1"><name>Мужские куртки</name><short>Мужские куртки</short></category>
<category id="2"><name>Мужские брюки</name><short>Мужские брюки</short></category>
<category id="3"><name>Мужские портянки</name><short>Мужские портянки</short></category>

您可以在正则表达式模式下尝试以下查找和替换:

Find:    <category id="(\d+)"><name>(.*?)</name><short></short></category>
Replace: <category id=""><name></name><short></short></category>

Demo

  • Ctrl+H
  • 查找内容:<category id="\d+"><name>(.*?)</name><short>\K(?=</short></category>)
  • 替换为:</code></li> <li><strong>检查</strong> <em>环绕</em></li> <li><strong>检查</strong> <em>正则表达式</em></li> <li><strong>取消选中</strong> <code>. matches newline
  • 全部替换

解释:

<category id="\d+"><name>       # literally
(.*?)                           # group 1, 0 or more any character but newline, not greedy
</name><short>                  # literally
\K                              # forget all we have seen until this position
(?=</short></category>)         positive lookbehind, zero length assertion that makes sure we have lose tags after

替换:

          # content of group 1, the text

屏幕截图(之前):

截图(后):