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.
Related
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
I have a (sorted) list like this and seek a regular expression to match consecutive duplicates.
1,1,1.28,1.35,1.4,1.4,2,2,4,7.5,7.56
I tried different options and the best so far was ?:^|,)([^,]+)(,[ ]*\1)+, but obviously it does not take into account cases like 1,1,1.28 (see demo).
In plain words, the regex I would need it:
Whatever there's inside two commas, match if there is a duplicate
You can use
(?<![^\D,])(\d+(?:\.\d+)?)(?:,\1)+(?![^,]|\.?\d)
Replace with $1. See the regex demo.
Details:
(?<![^\D,]) - immediately to the left of the current location, there can be no char other than a non-digit or comma
(\d+(?:\.\d+)?) - Group 1: one or more digits followed with an optional sequence of . and one or more digits
(?:,\1)+ - one or more sequences of a comma and Group 1 values
(?![^,]|\.?\d) - immediately to the right, there can't be a char other than a , or an optional . followed with a digit.
My take on this is:
(\b\d+(?:\.\d+)?\b)(?:,\1)+[^\.\d]
What's good about this one in particular is that it matches all the commas between the repeating numbers. That is handy in case you have to only retain one copy of a number in the list and delete all the others - you can simply delete the entire match and substitute it back with group 1 content, and the comma order will still be as expected - a,b,c! Or in case you need to remove duplicates entirely, just remove all matches (again, the order will be the same).
Explanation:
(\b\d+(?:\.\d+)?\b) matches a number, possibly a decimal fraction. "boundaries" are used in order not to match "...,11,1,...". This exact ordering of numbers is not allowed (11>1), but I inserted it just to make sure there will no problems of similar kind.
(?:,\1)+ matches a comma and then the previously found number. Here we use the fact that the numbers are sorted.
[^\.\d] is tricky: in case the first non-mathing number has a dot and the matching doesn't, we have to stop and do not match the dot. Also we have to not match "7.5,7.56", and for that we can use "not digit". But then we have to match everything else, including end of line. So as a substitute for "not digit AND not dot" I used "not (digit or dot)".
I Have some strings:
1:2:3:4:5
2:3:4:5
5:3:2
6:7:8:9:0
How to find strings that have exact numbers of colons?
Example I need to find strings where 4 colons.
Result:
1:2:3:4:5 and 6:7:8:9:0
Edit:
No matter what text between colons, it may so:
qwe:::qwe:
:998:qwe:3ee3:00
I have to specify a number of colons, but using regexp_matches.
It something like filter to search broken strings.
Thanks.
With N being the number you search for:
"^([^:]*:){N}[^:]*$"
Here is a test:
for s in ":::foo:bar" "foo:bar:::" "fo:o:ba::r" ; do echo "$s" | egrep "^([^:]*:){4}[^:]*$" ; done
Change 4 to 3 and 5, to see it not matching.
Maybe postgresql needs specific flags or masking for some elements.
"^([^:]*:){N}[^:]*$"
"^ $"
# matching the whole String/Line, not just a part
"([^:]*:){N}[^:]*"
"( ){N}[^:]*"
# N repetitions of something, followed by an arbitrary number of non-colons (maybe zero)
"([^:]*:)"
# non-colons in arbitrary number (including zero), followed by a colon
You want to use the quantifier syntax {4}. The quantifier is used to indicate that the preceding capture group needs to occur n number of times in order to meet the matching criteria. To find the pattern of five digits separated by semi-colons. something like the following would work.
((\d\:){4}\d)
I am assuming you may want any digit or word character but not whitespace or punctuation. In that case use the word character (\w).
((\w)[\:]){4}(\w))
But depending on what you would like to do with that pattern you may need a different regular expression. If you wanted to capture and replace all the colons while leaving the digits intact your pattern would need to use string replacement or more advanced grouping.
Any number of any characters, including 4 :
((.*:){4}.*)
I have a regex from a previous thread. This regex seems to only work in some numbers and I'm a total noob to regex and coding. Is there anyway to make all numbers in the text I have reversed? The max total of digits in my text is probably 10 digits in some lines, and in other maybe just 2, sometimes 3 or 4, but max is 10, I'll give examples.
goku144
r3apt0r
66958496
Veg3ta123
The regex I have only makes some of the numbers reversed, not all, is there anyway to fix the regex provided by horucrux.
Regex below and link
How to make numbers backwards in notepad++
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
Same method as for the other question:
Step 1. Add a marker for the not-yet-inverted digits.
Find:
\d
Replace:
§$0
Step 2. Do Replace all enough times for inverting digits and, in the same time, removing markers:
Find:
§(\d)([^\n]*)§(\d)
Replace:
$3$2$1
Step 3. Delete remaining markers (the ones for central digits).
Find:
§(\d)
Replace:
$1
Since you deal with notepad++, it is very important to determine maximum length of the sequence as we have no power of programming languages.
Also it would be great to know if you want to reverse any sequence of digits -- in substrings starting with digits, digits in the middle, digits in the end.
Say you want to reverse ALL sequences of digits in your file and the longest sequence has 5 digits. Then:
find: (?<=\D)(\d)(\d)(\d)?(\d)?(\d)?(?=\D)
replace: $5$4$3$2$1
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