Regex 如何在'=';登录记事本中的所有行++;

Regex 如何在'=';登录记事本中的所有行++;,regex,notepad++,programmers-notepad,Regex,Notepad++,Programmers Notepad,我的文件内容如下 abcd-12=jksjd-jkkj xyzm-87=hjahf-tyewg-iuoiurew zaqw-99=poiuy-12hd-jh-12-kjhk-4rt45 我想在等式的R.H.S上用“=”后面的下划线符号替换hypen No of hypenated terms are variable in the lines, it can be 3 or 4 or 5 如何对整个文档执行此操作。左手边应该完好无损 我期望的结果是: abcd-12=jksjd_jkkj x

我的文件内容如下

abcd-12=jksjd-jkkj
xyzm-87=hjahf-tyewg-iuoiurew
zaqw-99=poiuy-12hd-jh-12-kjhk-4rt45
我想在等式的R.H.S上用“=”后面的下划线符号替换hypen

No of hypenated terms are variable in the lines, it can be 3 or 4 or 5
如何对整个文档执行此操作。左手边应该完好无损

我期望的结果是:

abcd-12=jksjd_jkkj
xyzm-87=hjahf_tyewg_iuoiurew
zaqw-99=poiuy_12hd_jh_12_kjhk_4rt45

一个选项是在正则表达式模式下进行以下查找和搜索:

Find:    = ([^-]+)-([^-]+)$
Replace: = $1_$2

这里的策略是匹配并捕获出现在等式右侧的连字符项的两半。然后,替换为用下划线分隔的两半

编辑:

(?:             # non capture group
    =           # equal sign
  |             # OR
    (?!^)       # negative lookahead, make sure we are not at the beginning of a line
    \G          # restart from last match position
)               # end group
.*?             # 0 or more any character but newline, not greedy
\K              # forget all we have seen until this position
-               # a hyphen
如果RHS确实有四个连字符术语,则使用:

Find:    = ([^-]+)-([^-]+)-([^-]+)-([^-]+)$
Replace: = $1_$2_$3_$4
搜索:

(=[^-\r\n]+)-
替换为:

\1_
注意:重复搜索和替换,直到不再进行替换


测试。

这将在单程中替换任意数量的连字符:

  • Ctrl+H
  • 查找内容:
    (?:=|(?!^)\G.*?\K-
  • 替换为:
  • 检查环绕
  • 检查正则表达式
  • 取消选中
    。匹配换行符
  • 全部替换
说明:

(?:             # non capture group
    =           # equal sign
  |             # OR
    (?!^)       # negative lookahead, make sure we are not at the beginning of a line
    \G          # restart from last match position
)               # end group
.*?             # 0 or more any character but newline, not greedy
\K              # forget all we have seen until this position
-               # a hyphen
屏幕截图(之前):

(?:             # non capture group
    =           # equal sign
  |             # OR
    (?!^)       # negative lookahead, make sure we are not at the beginning of a line
    \G          # restart from last match position
)               # end group
.*?             # 0 or more any character but newline, not greedy
\K              # forget all we have seen until this position
-               # a hyphen

屏幕截图(之后):

(?:             # non capture group
    =           # equal sign
  |             # OR
    (?!^)       # negative lookahead, make sure we are not at the beginning of a line
    \G          # restart from last match position
)               # end group
.*?             # 0 or more any character but newline, not greedy
\K              # forget all we have seen until this position
-               # a hyphen

你太棒了!!!谢谢你,伙计。。我将接受你的回答。。它告诉我再等5分钟。。。然而,我还有一个问题。。我在RHS中有多个催眠文本。您的解决方案是替换第一个匹配项而忽略其余匹配项。下面是我的输入:abcd-12=jksjd-jkkj-kjk-766 xyzm-87=hjahf-tyewg-766-gffg zaqw-99=POUY-12hd-0909-iuy@Som如果连字符术语的数量确实是可变的(例如,它可能是,或3,或4),那么这在NPP中可能很棘手。我用4个术语的选项更新了我的答案。如果有4个或更少的被催眠术语,你的答案是完美的。所以我们需要检查催眠项的最大数量是多少,并给出更多的计数。我认为它会这样工作。你对Python这样的东西开放吗?在那里处理起来会容易得多。这会用更新的正则表达式取代R.H.S.上的多个被夸大的术语吗。最初,它在L.H.S.上做了一些额外的替换。它将替换任意数量的破折号,您只需不断替换,直到所有破折号都被替换。我刚刚在NPPP中测试过。最好的答案。完美的没有瑕疵。这真的很复杂+1。我已将此问题添加到我的收藏夹中。
\K
的使用非常有趣。@TimBiegeleisen:谢谢<代码>\K的作用类似于可变长度的查找