【问题标题】:How can I use regular expressions to find a word and copy it to a specific location?如何使用正则表达式查找单词并将其复制到特定位置?
【发布时间】:2021-09-25 00:59:20
【问题描述】:

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

例子:

<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>

【问题讨论】:

    标签: notepad++ sublimetext3 nsregularexpression


    【解决方案1】:

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

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

    Demo

    【讨论】:

      【解决方案2】:
      • Ctrl+H
      • 查找内容:&lt;category id="\d+"&gt;&lt;name&gt;(.*?)&lt;/name&gt;&lt;short&gt;\K(?=&lt;/short&gt;&lt;/category&gt;)
      • 替换为:$1
      • 检查 环绕
      • CHECK 正则表达式
      • 取消选中 . 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
      

      替换:

      $1          # content of group 1, the text
      

      屏幕截图(之前):

      截图(之后):

      【讨论】:

        猜你喜欢
        • 2022-08-18
        • 2019-08-24
        • 2022-07-14
        • 1970-01-01
        • 2017-09-29
        • 2010-11-17
        • 1970-01-01
        • 2014-09-01
        相关资源
        最近更新 更多