Regex to invert search in Notepad++ - regex

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
\d\d\s[m][s]
What I want to do is search for string prior to 18 ms in Notepad++ and then delete it. 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. For example it should be able to search for 18 ms as well as 999 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:
\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)
See demo
{2,3} is a limiting quantifier that lets you match 2 or 3 preceding subpattern.
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.
You can replace with empty string and 18 ms will stay on the line.
Note you can use \d+ to allow 1 or more digits to be matched (without restrictions on the digit number).
Note 2: if your number is the first of many on the line you need to use lazy matching, i.e. use .*? 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.
.*?(?=\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

Related

Notepad++ replace specific number with up to it's first 4 digit

I want to find those number which contains more than 5 digits and replace it with first 4 digits.
Used below Regex to find number which contains more than 5 digits.
[0-9]{5,}
How Can I achieve blow output?
99999999 -> this will replace with 9999
12345.66 -> this will replace with 1234.66
1234 -> Remains unchanged
This one should do it:
The regex
([0-9]{4})[0-9]+
takes the four numbers as first (and only) group
requires at lease one more number behind
replaces the complete match with the first (and only) group
Using notepad++, you can match 4 digits, then use \K to clear the current output buffer and match 1 or more digits.
\d{4}\K\d+
See a regex demo.
In the replacement use an empty string.
If you don't want partial matches, you can add word boundaries \b around the pattern.
\b\d{4}\K\d+\b
See another regex demo

RegEx to check 24 hours time format fails

I have the following RegEx that is supposed to do 24 hours time format validation, which I'm trying out in https://rubular.com
/^[0-23]{2}:[0-59]{2}:[0-59]{2}$/
But the following times fails to match even if they look correct
02:06:00
04:05:00
Why this is so?
In character classes, you're supposed to denote the range of characters allowed (in contrast to the numbers you want to match in your example). For minutes and seconds, this is relatively straight-forward - the following expression
[0-5][0-9]
...will match any numerical string from "00" to "59".
But for the hours, you need to two separate expressions:
[01][0-9]|2[0-3]
...one to match "00" to "19" and one to match "20" to "23". Due to the alternative used (| character), these need to be grouped, which adds another bit of syntax (?:...). Finally we're just adding the anchors ^ and $ for beginning and end of string, which you already had where they belong.
^(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$
You can check this solution out at regex101, if you like.
Your problem is that you understand characters ranges wrong: 0-23 doesn't mean "match any number from 0 to 23", it means: 0-2- match one digit: 0,1 or 2, then match 3.
Try this pattern: (?:[01][0-9]|2[0-3])(?::[0-5][0-9]){2}
Explanation:
(?:...) - non-capturing group
[01][0-9]|2[0-3] - alternation: match whether 0 or one followed by any digits fro 0 to 9 OR 2 followed by 0, 1, 2 or 3 (number from 00-23)
(?::[0-5][0-9]){2} - match : and [0-5][0-9] (basically number from 00-59) twice
Demo
use this (([0-1]\d|[2][0-3])):(([0-5][0-9])):(([0-5][0-9]))
Online demo

How to make numbers backwards in notepad++

So, I have a lot of numbers in lines like so
rocket123
firefly1000
attack577
Is there any regex to make the numbers reversed?
rocket321
firefly0001
attack775
This is feasible with a little trick.
Step 1. Add a marker for the not-yet-inverted digits.
Find:
\b(\w+?)(\d+)\b
Replace:
$1§$2
You can choose other marker instead of §.
Step 2. Do Replace all enough times with these settings:
Find:
\b(\w+)§(\d*)(\d)\b
Replace:
$1$3§$2
Step 3. Delete all markers.
Find:
\b(\w+\d)§
Replace:
$1
Hope this helps.
If the maximum number of digits to be reversed is known and not too large then a single Notepad++ regular expression search and replace can be used. Suppose the maximum number of digits is 12 then the expressions are:
Search for:
(\d)(\d)(\d)?(\d)?(\d)?(\d)?(\d)?(\d)?(\d)?(\d)?(\d)?(\d)?
Replace with:
(?{12}${12})(?{11}${11})(?{10}${10})(?9$9)(?8$8)(?7$7)(?6$6)(?5$5)(?4$4)(?3$3)$2$1
Explanation:
Any number to be reversed must have at least two digits, so the initial (\d)(\d) in the search gets two digits and the final $2$1 in the replace puts them in reverse order at the end of the output. (The first two digits are the easy part.) The search string then repeats the pattern (\d)? as many times as needed for the maximum number of digits. These match the remaining digits, if any. Each of these (\d)? patterns has a corresponding item in the replace string, they are of the form (?N$N) where each N is the number of the capture group. Single digit captures are like (?4$4) for number 4. For captures 10 and above the number is wrapped in curly braces, such as (?{12}${12}) for number 12. These replacement items test whether a capture group captured anything and, if it did then insert that captured item. See also this answer.
Variations
Add or remove additional search and replacement items as needed for longer or shorter maximum numbers of digits.
If the number of digits might be larger than expected then adding an extra (\d)? to the search string and (?{13}__Some suitable error message__) to the end of the replacement will output the error message on overlong groups of digits. Of course the 13 needs to be altered to match the number of items in search and replacement.
Tested with Notepad++ version 7.5.6.

Limit number of character of capturing group

Let's say i have this text : "AAAA1 AAA11 AA111AA A1111 AAAAA AAAA1111".
I want to find all occurrences matching these 3 criteria :
-Capital letter 1 to 4 times
-Digit 1 to 4 times
-Max number of characters to be 5
so the matches would be :
{"AAAA1", "AAA11", "AA111", "A1111", "AAAA1"}
i tried
([A-Z]{1,4}[0-9]{1,4}){5}
but i knew it would fail, since it's looking for five time my group.
Is there a way to limit result of the groups to 5 characters?
Thanks
You can limit the character count with a look ahead while checking the pattern with you matching part.
If you can split the input by whitespace you can use:
^(?=.{2,5}$)[A-Z]{1,4}[0-9]{1,4}$
See demo here.
If you cannot split by whitespace you can use capturing group with (?:^| )(?=.{2,5}(?=$| ))([A-Z]{1,4}[0-9]{1,4})(?=$| ) for example, or lookbehind or \K to do the split depending on your regex flavor (see demo).
PREVIOUS ANSWER, wrongly matches A1A1A, updated after #a_guest remark.
You can use a lookahead to check for your pattern, while limiting the character count with the matching part of the regex:
(?=[A-Z]{1,4}[0-9]{1,4}).{2,5}
See demo here.

Regular expression non-capturing group character length

I'm trying to generate a regular expression with the next pattern.
A number, of a maximum of 16 digits, that can have or not a comma inside, never at the beginning, never at the end.
I tried this:
^(?:\d+(?:,{1}\d+){1})$
The problem is that I can't count the result of a group {0,16}.
This is a list of numbers that should fit the expression:
123,34
1,33333333
1222222233
Example numbers that shouldn't fit:
1111,1111,1111
,11111
11111,
11111111111111111111111111111,111111111111111 (more than 16
characters)
You may check the length before that or using ^(?=[\d,]{1,16}$)(?:\d+(?:,\d+)?)$
That is a lookahead that checks the length before doing the real match.
If your regex flavour supports lookahead assertions, you can use this:
^(?!(?:\d{17,}$|[,\d]{18,}$))(?:\d+(?:,\d+)?)$
See it here on Regexr
I removed the superfluous {1} and made the group with the fraction optional.
The negative lookahead assertion (?!(?:\d{17,}$|[,\d]{18,}$)) is checking your length requirement. It fails if it finds 17 or more digits till the end OR 18 or more digits and commas till the end. That I allow multiple commas in the character class here is not a problem, that there is only one comma is ensured by the following pattern.