正则表达式在 Notepad++ 中反转搜索

Regex to invert search in Notepad++

我有一个字符串

2012-02-19 00:11:12,128|DEBUG|Thread-1|@@@ Time taken is 18 ms 

下面的正则表达式允许我搜索 18 ms

\d\d\s[m][s]

我想做的是在 Notepad++ 中搜索 18 ms 之前的字符串,然后将其删除。因此,在我拥有的数千行中,我可以提取出时间。

此外,我需要上面提到的正则表达式来处理 3 位数和 2 位数的计时。例如,它应该能够搜索 18 ms 以及 999 ms.

请帮忙。

您可以将正则表达式置于正前瞻中:

^.*(?=\d{2,3}\sms\s*$)

如果您在 18 ms 之后有一些文字,您需要使用 word boundary \b:

\b allows you to perform a "whole words only" search using a regular expression in the form of \bword\b

^.*(?=\d{2,3}\sms\b)

demo

{2,3} 是一个 limiting quantifier,可让您匹配前面的 2 个或 3 个子模式。

There's an additional quantifier that allows you to specify how many times a token can be repeated. The syntax is {min,max}, where min is zero or a positive integer number indicating the minimum number of matches, and max is an integer equal to or greater than min indicating the maximum number of matches. If the comma is present but max is omitted, the maximum number of matches is infinite.

您可以替换为空字符串,18 ms 将保持在线状态。

注意可以使用\d+来匹配1个或多个数字(不限制数字个数)。

注意2:如果你的号码是很多号码中的第一个,你需要使用惰性匹配,即使用.*? 而不是模式开头的 .*

Also, I need regex mentioned above to work with timings which are in 3 digits as well as 2 digits.

.*?(?=\d{2,3}\sms\b)

使用上面的正则表达式,然后用空字符串替换匹配项。

您可以使用 capturing group:

查找:

^.*(\d{2,}\s[m][s])$

替换为: