Fro*_*III 1 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
Run Code Online (Sandbox Code Playgroud)
结果:
AAA12345
AAA22345
BBB32345
XYZ42345
XYZ52345
Run Code Online (Sandbox Code Playgroud)
要搜索的正则表达式:
([0=9])([0-9]{4})
Run Code Online (Sandbox Code Playgroud)
正则表达式替换为:
(SOME WAY OF DECIDING BETWEEN AAA|BBB|XYZ depending on \1!)\1\2
Run Code Online (Sandbox Code Playgroud)
这可能吗?
您不需要在此处使用正则表达式条件功能(即:(?(condition)true|false)
.
您可以使用您需要的数据创建一个假的最后一行(末尾没有换行符):
Blah 12345 Blah
Blah 22345 Blah
Blah 32345 Blah
Blah 42345 Blah
Blah 52345 Blah
AAA#BBB#XYZ
Run Code Online (Sandbox Code Playgroud)
并使用此模式:
^[^0-9\n]*(?:((?|[12](?=(?>.*\n)*([^#]+))|3(?=(?>.*\n)*[^#]*#([^#]+))|[45](?=(?>.*\n)*(?:[^#]*#){2}([^#]+)))\d+).*|.*\z)
Run Code Online (Sandbox Code Playgroud)
和这个替换:
$2$1
Run Code Online (Sandbox Code Playgroud)
图案细节:
^ # 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
)
Run Code Online (Sandbox Code Playgroud)