Notepadd ++ Insert hyphen if more than 5 numbers - regex

I have a large list of zip codes. Most of the zip codes are 5 digits but some are 9 digits.
I need to insert a hyphen after the 5th number but only if there are more than 5 numbers.
I can find those with 9 digits with
(^\d{9})
but I am unsure on how to replace.

With capturing groups and substitution we can achieve this.
Find what: (\d{5})(\d{4})
Replace with: $1-$2
This finds 5 digits followed by 4 digits and creates two capturing groups (one chunk with the first 5 digits and another chunk with the following 4). The '$'-sign followed by a number is a substitution. Here we are saying: Paste the first capturing group, insert a hyphen and paste the second capturing group.
Example: https://regex101.com/r/KnzTus/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

use ultraedit find and replace Perl regex to insert colon into 4 digit time string

I have multiple 24-hour time strings through several files. For example, 1234, which I wish to replace with 12:34.
Finding them is easy, just \d\d\d\d, that I understand and it works. However, what replace string do I need. In other words, say xx:xx, what do I put in place of each x.
I've tried numbers of things to no avail. I'm obviously not understanding how I get it to remember the digits it found and to recall them in the replace string.
If in your example data 4 digits represent 24 hour time strings you could match 2 capturing groups between word boundaries to prevent a match with more then 4 digits. You can Adjust the word boundaries to your requirements.
Match
\b(\d{2})(\d{2})\b
Replace
group1:group2 \1:\2
Explanation
\b Match a word boundary
(\d{2}) Capture in a group 2 digits
(\d{2}) Capture in a group 2 digits
\b Match a word boundary
Note
Matching 4 digits does not verify a valid 24 hour time. You could match that using for example \b([01][0-9]|2[0-3])([0-5][0-9])\b and replace with \1:\2

regex to match the last five numbers in a row

I need a regular expression to find the last occurrence of 5 consecutive digits in a string. This is what I have right now:
([0-9]{5})[a-zA-Z]*$
This only matches some of my test strings.
In a live environment the numbers will change, but for testing I expect to capture the substring '12345' in each of the test strings below:
D012345
D012345AS
D012345RM-67
D12345D
12345D67
TEST-Str12345ing-rm6
Updated
Works w/ global flag, passes all tests. No capture group required.:
[0-9]{5}(?![0-9])(?!.*[0-9]{5})
Live example:
http://www.regexr.com/3c875
Let's break this down:
Match any instance of five digits
[0-9]{5}
But the instance cannot be immediately followed by another digit - this way,
we always get the last five in any group of consecutive numbers.
(?![0-9])
Lastly, make sure no further groups of consecutive numbers exist that have
more than five digits.
(?!.*[0-9]{5})
You can use this regex:
.*([0-9]{5})
to make sure you're matching last 5 continuous digits in the input. Matched 5 digits are available in captured group #1.
.* (greedy match) at start makes sure that we match very last 5 digits only.
RegEx Demo

Logback: replacing 10 digit number by ******** and last two digit

I use the following in my pattern(logback.xml) to replace 10 digit numbers in my log.
%replace(%msg){'\d{10}','**********'}
One problem with this approach is, it also matches first 10 digits of 11 digit number.
Is there a way to match exactly 10 digits numbers.
Now the bigger problem is somehow I need to display the last two digits of this 10 digit number.
Use this:
%replace(%msg){'\b\d{10}\b','**********'}
\b is a word boundary that matches a position where one side is a letter, and the other side is not a letter (for instance a space character, or the beginning of the string)
To display (leave uncaptured) the last two digits, please see the following regex:
'\b\d{8}(?=\d{2}\b)'
View a regex demo!
This will find 8 numerical digits before two digits where the 10 digits are wrapped within word boundaries. Since (?= ) is positive lookahead assertion, it won't be matched. The entire match can then be replaced with:
********
No capturing groups necessary.
To get the last two digits of the corressponding 10 digit number,
'\b\d{8}(\d{2})\b'
First captured group contains the last two digits.
DEMO
Yes, if you use this (as #zx81 said):
\b\d{8}(\d\d)\b
(Explenation: http://www.regexper.com/#%5Cb%5Cd%7B8%7D(%5Cd%5Cd)%5Cb)
That will find 10 digits and store the last 2 digits in a group. If you replace that with a string like this:
********$1
That will replace the first 8 numbers, and leave the last two visible.
Example: http://regexr.com/3989s

Regex 2 digits separated by commas, not all required

I need a regex for the following input:
[2 digits], comma, [two digits], comma, [two digits]
The 2 digits cannot start with 0. It is allowed to only enter the first 2 digits. Or to enter the first 2 digits, then a comma en then the next 2 digits. Or to enter the full string as described above.
Valid input would be:
10
99
17,56
15,99
10,57,61
32,44,99
Could anyone please help me with this regex?
At the moment I have this regex, but it doesn't limit the input to maximum 3 groups of 2 digits:
^\d{2}(?:[,]\d{2})*$
^[1-9]\d(?:,[1-9]\d){0,2}$
The first part ([1-9]\d) is simply the first number, which has to be present at all times. It consists of a non-zero digit and an arbitrary second digit (\d).
What follows is a non-capturing group ((?:...)), containing a comma followed by another two-digit number (,[1-9]\d), just alike the first one. This group can be repeated between zero and two times ({0,2}), so you get either no, one or two sequences of a comma and another number.
You can easily expand the part in the curly braces to allow for more allowed numbers.
^[1-9]\d([,][1-9]\d){0,2}$