简体   繁体   English

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

[英]Regex to invert search in Notepad++

I have a string 我有一个弦

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

Below regex allows me to search for 18 ms 下面的正则表达式允许我搜索18 ms

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

What I want to do is search for string prior to 18 ms in Notepad++ and then delete it. 我想做的是在Notepad ++中搜索18 ms之前的字符串,然后将其删除。 So that out of thousands rows I have, I can just extract out timings. 因此,在我拥有的数千行中,我可以提取出时间。

Also, I need regex mentioned above to work with timings which are in 3 digits as well as 2 digits. 另外,我需要上面提到的正则表达式来处理3位和2位的计时。 For example it should be able to search for 18 ms as well as 999 ms . 例如,它应该能够搜索18 ms999 ms

Please help. 请帮忙。

You may put your regex into a positive lookahead: 您可以将正则表达式置于积极的前瞻状态:

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

In case you have some text after 18 ms , you need to use a word boundary \\b : 如果在18 ms之后有一些文本,则需要使用单词边界\\b

\\b allows you to perform a "whole words only" search using a regular expression in the form of \\bword\\b \\b允许您使用\\bword\\b形式的正则表达式执行“仅全词”搜索

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

See demo 观看演示

{2,3} is a limiting quantifier that lets you match 2 or 3 preceding subpattern. {2,3}是一个限制量词 ,可让您匹配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. 语法为{min,max} ,其中min为零或表示最小匹配数的正整数,并且max是等于或大于min的整数,表示最大匹配数。 If the comma is present but max is omitted, the maximum number of matches is infinite. 如果存在逗号但省略了max ,则最大匹配数是无限的。

You can replace with empty string and 18 ms will stay on the line. 您可以用空字符串替换,并且18 ms会保持在线状态。

Note you can use \\d+ to allow 1 or more digits to be matched (without restrictions on the digit number). 请注意,您可以使用\\d+允许匹配1个或多个数字(对数字编号没有限制)。

Note 2 : if your number is the first of many on the line you need to use lazy matching , ie use .*? 注意2 :如果您的电话号码是在线电话中的第一个电话号码,则需要使用惰性匹配 ,即使用.*? instead of .* in the beginning of pattern. 而不是模式开头的.*

Also, I need regex mentioned above to work with timings which are in 3 digits as well as 2 digits. 另外,我需要上面提到的正则表达式来处理3位和2位的计时。

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

Use the above regex and then replace the match with empty string. 使用上面的正则表达式,然后将匹配项替换为空字符串。

You can use capturing group : 您可以使用捕获组

Find: 找:

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

Replace with: 用。。。来代替:

\1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM