我认为我的正则表达能力很强,但我已经被压垮了。
我有几行格式
Time: 105 0 0
Time: 88 0 1
Time: 44 1 1
Time: 64 1 0
我希望这些内容变成这样:
Time: 105 thread00
Time: 88 thread01
Time: 44 thread11
Time: 64 thread10
我可以匹配[0-9][ ][0-9]
部分...我将它与那个正则表达式匹配!
但我不知道如何保留值并删除空格。用新东西替换它,当然......但我如何保存价值?
答案 0 :(得分:3)
查找内容: (\d)\s(\d)$
替换为: thread\1\2
\d
匹配任何数字,\s
匹配任何空格字符。
将捕获括号以用作\1
,\2
,\3
...并且\0
将提供整个匹配。*
$
匹配一行的结尾,这样就不会意外地匹配第一行中的“5 0”。
*请注意,某些正则表达式引擎使用\1
模式,而其他一些则使用$1
。 Notepad ++使用前者。
答案 1 :(得分:2)
你可以试试这个:
<强>模式:强>
/^(.*)(\d+)\s(\d+)$/
故障:
^ # start of line
(.*) # the first part of the line -- capture $1
(\d+) # the first number (1 or more) -- capture $2
\s # the space between the numbers
(\d+) # the second number (1 or more) -- capture $3
$ # end of line
<强>替换强>
/$1thread$2$3/
<强>结果:强>
Time: 105 thread00
Time: 88 thread01
Time: 44 thread11
Time: 64 thread10