Regex 条件正则表达式替换字符串不在初始表达式中的位置

Regex 条件正则表达式替换字符串不在初始表达式中的位置,regex,replace,notepad++,Regex,Replace,Notepad++,我将仅举几个例子,因为我认为这是描述我想要的内容的最佳方式,但基本上我只想保留数字,但根据第一个数字(1或2:AAA、3:BBB、4或5:XYZ)将文本前置到开头: 初始字符串: Blah 12345 Blah Blah 22345 Blah Blah 32345 Blah Blah 42345 Blah Blah 52345 Blah 结果: AAA12345 AAA22345 BBB32345 XYZ42345 XYZ52345 要搜索的正则表达式: ([0=9])([0-9]{4})

我将仅举几个例子,因为我认为这是描述我想要的内容的最佳方式,但基本上我只想保留数字,但根据第一个数字(1或2:AAA、3:BBB、4或5:XYZ)将文本前置到开头:

初始字符串:

Blah 12345 Blah
Blah 22345 Blah
Blah 32345 Blah
Blah 42345 Blah
Blah 52345 Blah
结果:

AAA12345
AAA22345
BBB32345
XYZ42345
XYZ52345
要搜索的正则表达式:

([0=9])([0-9]{4})
将正则表达式替换为:

(SOME WAY OF DECIDING BETWEEN AAA|BBB|XYZ depending on \1!)\1\2

这可能吗?

使用记事本++似乎确实有一种方法可以做到这一点,因为它支持条件替换,这在这个问题的公认答案中有详细说明:


但请注意,对于您的每一个案例,只需执行多个查找/替换操作可能会更简单、更快……

似乎确实有一种方法可以使用Notepad++实现这一点,因为它支持条件替换,这一点在该问题的公认答案中有详细说明:

但请注意,只需为每个案例执行多个查找/替换操作,可能会更容易、更快速……

使用

^.* ((?:([12])|(3)|([45]))\d{4}) .*
并替换为

(?2AAA:(?3BBB:XYZ))$1
模式匹配:

  • ^
    -行首
  • *
    -零个或多个字符,直到最后一个换行符除外
  • ((?:([12])|(3)([45]))\d{4}
    -一个空格,一个特定数字(
    1
    2
    )-第2组,
    3
    -第3组,
    4
    -第4组和4个以上数字
  • *
    -行的其余部分
更换模式详情:

^ # start of a line
[^0-9\n]* # all that isn't a digit
(?:
    ( # first capture group for the number
        (?| # branch reset: all capture have the same number inside (2 here)
            [12]
            (?= # lookahead to reach the good string
                (?>.*\n)* # reach the last line
                ([^#]+)   # capture the first string
            )
          |
            3
            (?=
                (?>.*\n)*
                [^#]*#    # skip the first string
                ([^#]+)   # capture the second
            )
          | # same thing
            [45](?=(?>.*\n)*(?:[^#]*#){2}([^#]+))
        ) # close the branch reset group
        \d+
     ) # close the capture group 1
     .* # match the end of the line
  |
    .*\z # match the last line
)
  • (?2
    -如果第二组匹配,
    • AAA
      -添加
      AAA
    • -或
    • (?3
      -如果第3组匹配,
      • BBB
        -插入
        BBB
      • -或
      • XYZ
        -插入
        XYZ
  • $1
    -并插入第1组内容(整个5位数字)
使用

^.* ((?:([12])|(3)|([45]))\d{4}) .*
并替换为

(?2AAA:(?3BBB:XYZ))$1
模式匹配:

  • ^
    -行首
  • *
    -零个或多个字符,直到最后一个换行符除外
  • ((?:([12])|(3)([45]))\d{4}
    -一个空格,一个特定数字(
    1
    2
    )-第2组,
    3
    -第3组,
    4
    -第4组和4个以上数字
  • *
    -行的其余部分
更换模式详情:

^ # start of a line
[^0-9\n]* # all that isn't a digit
(?:
    ( # first capture group for the number
        (?| # branch reset: all capture have the same number inside (2 here)
            [12]
            (?= # lookahead to reach the good string
                (?>.*\n)* # reach the last line
                ([^#]+)   # capture the first string
            )
          |
            3
            (?=
                (?>.*\n)*
                [^#]*#    # skip the first string
                ([^#]+)   # capture the second
            )
          | # same thing
            [45](?=(?>.*\n)*(?:[^#]*#){2}([^#]+))
        ) # close the branch reset group
        \d+
     ) # close the capture group 1
     .* # match the end of the line
  |
    .*\z # match the last line
)
  • (?2
    -如果第二组匹配,
    • AAA
      -添加
      AAA
    • -或
    • (?3
      -如果第3组匹配,
      • BBB
        -插入
        BBB
      • -或
      • XYZ
        -插入
        XYZ
  • $1
    -并插入第1组内容(整个5位数字)

此处不需要使用regex条件功能(即:
(?(条件)true | false)

您可以使用所需的数据创建一个伪最后一行(末尾没有换行符):

Blah 12345 Blah
Blah 22345 Blah
Blah 32345 Blah
Blah 42345 Blah
Blah 52345 Blah
AAA#BBB#XYZ
并使用此模式:

^[^0-9\n]*(?:((?|[12](?=(?>.*\n)*([^#]+))|3(?=(?>.*\n)*[^#]*#([^#]+))|[45](?=(?>.*\n)*(?:[^#]*#){2}([^#]+)))\d+).*|.*\z)
而这个替代品:

$2$1

图案详情:

^ # start of a line
[^0-9\n]* # all that isn't a digit
(?:
    ( # first capture group for the number
        (?| # branch reset: all capture have the same number inside (2 here)
            [12]
            (?= # lookahead to reach the good string
                (?>.*\n)* # reach the last line
                ([^#]+)   # capture the first string
            )
          |
            3
            (?=
                (?>.*\n)*
                [^#]*#    # skip the first string
                ([^#]+)   # capture the second
            )
          | # same thing
            [45](?=(?>.*\n)*(?:[^#]*#){2}([^#]+))
        ) # close the branch reset group
        \d+
     ) # close the capture group 1
     .* # match the end of the line
  |
    .*\z # match the last line
)

这里不需要使用regex条件特性(即:
(?(条件)true | false)

您可以使用所需的数据创建一个伪最后一行(末尾没有换行符):

Blah 12345 Blah
Blah 22345 Blah
Blah 32345 Blah
Blah 42345 Blah
Blah 52345 Blah
AAA#BBB#XYZ
并使用此模式:

^[^0-9\n]*(?:((?|[12](?=(?>.*\n)*([^#]+))|3(?=(?>.*\n)*[^#]*#([^#]+))|[45](?=(?>.*\n)*(?:[^#]*#){2}([^#]+)))\d+).*|.*\z)
而这个替代品:

$2$1

图案详情:

^ # start of a line
[^0-9\n]* # all that isn't a digit
(?:
    ( # first capture group for the number
        (?| # branch reset: all capture have the same number inside (2 here)
            [12]
            (?= # lookahead to reach the good string
                (?>.*\n)* # reach the last line
                ([^#]+)   # capture the first string
            )
          |
            3
            (?=
                (?>.*\n)*
                [^#]*#    # skip the first string
                ([^#]+)   # capture the second
            )
          | # same thing
            [45](?=(?>.*\n)*(?:[^#]*#){2}([^#]+))
        ) # close the branch reset group
        \d+
     ) # close the capture group 1
     .* # match the end of the line
  |
    .*\z # match the last line
)


我环顾了一下四周,但我发现的所有内容都会用搜索字符串中的内容替换文本,并想知道这是否可行。提前感谢各位。:)一点建议各位-所有伟大的解决方案(全部投赞成票).就像Nadia的答案一样正确,她是第一个;Wiktor的答案正确,更详细;Casimir的答案正确,不需要记事本++(我真的很喜欢)。通常,当每个人都正确时,我会给出“答案”对这个人来说,这是最低劣的,因为开始很困难。听起来公平吗?谢谢你的帮助!我同意。而且,自从雷沃删除了他的答案后,我投了他另外两个答案,所以我们都很高兴。我已经环顾了一下,但我发现的一切都用搜索字符串中的东西来代替文本,并且想知道这个wa是否这是可能的。提前感谢各位。:)一点建议各位-所有伟大的解决方案(全部投赞成票)。像Nadia的,因为它是正确的答案,她是第一个;Wiktor的,因为它是正确的,并进入了更多的细节;Casimir的,因为它是正确的,不需要记事本++(我真的很喜欢)。通常,当每个人都是正确的时候,我会给出”“回答"对那个人来说,这是最低的信任,因为一开始就很困难。这听起来公平吗?感谢所有的帮助!我同意。而且,由于revo删除了他的答案,我对他的其他两个答案投了更高的票,所以我希望我们都很高兴。我删除了我的答案,因为这不是唯一一个有独特解决方案的答案,我将对你的答案投更高的票,因为它不同,并且有一个独特的答案主意。哇,卡西米尔,这完全搞乱了我的脑袋——等我有更多时间的时候,我会更仔细地看这个,但非常有趣,谢谢!)@revo:谢谢,我认为你的解决方案是好的,我不知道在记事本++中使用条件替换是可能的。我删除了我的答案,因为它不是唯一一个具有唯一解决方案的答案,并且将对你的答案进行投票,因为它是不同的,并且有一个想法。哇,Casimir,这完全搞乱了我的脑袋-我会更仔细地看这个当我有更多时间,但非常有趣的时候,谢谢!:)@revo:谢谢,我认为你的解决方案是好的,我不知道可以在记事本++中使用有条件替换。谢谢Nadia-在这本书的背面写下我自己的,它可以工作!:)谢谢娜迪亚——在这本书的背面写下了我自己的作品,它很有效!:)谢谢Wiktor,这正是我想要的。:)很高兴你用我的另一个答案来构建你自己的模式:)谢谢Wiktor,没错