Notepad++ Replace regex match for same text plus appending character - regex

I have a file with text and numbers with a length of five (i.e. 12000, 11153, etc.). I want to append all of these numbers with a 0. So 11153 becomes 111530. Is this possible in Notepad++?
I know I can find all numbers with the following regex: [0-9]{5}, but how can I replace these with the same number, plus an appending 0?
In the replacement box I tried the following things:
[0-9]{5}0 - Which it took literally, so 11153 was replaced with [0-9]{5}0
\10 - I read somewhere that \1 would take the match, but it doesn't seem to work. This will replace 11153 with 0
EDIT: \00 - Based on this SO answer I see I need to use \0 instead of \1. It still doesn't work though. This will replace 11153 with
So, I've got the feeling I'm close with the \1 or \0, but not close enough.

You are very near to the answer! What you missed is a capturing group.
Use this regex in "Find what" section:
([0-9]{5})
In "Replace with", use this:
\10
The ( and ) represent a capturing group. This essentially means that you capture your number, and then replace it with the same followed by a zero.

You are very close. You need to add a capturing group to your regex by surrounding it with brackets. ([0-9]{5})
Then use \10 as the replacement. This is replacing the match with the text from group 1 followed by a zero.

You can use \K to reset.
\b\d{5}\b\K
And replace with 0
\b matches a word boundary
\d is a short for digit [0-9]
See demo at regex101

Related

Notepad++: Can I use regex to find some values and remove only one character instead of the whole pattern?

I want to use regex in notepad to find this pattern: "[0-9]+[\.][0-9]+[,][0-9]+" e.g. 1.010,80260
However from these kind of numbers I just want to remove the '.' , so the new value should be 1010,80260 .
So far I can only replace the whole pattern. Is there a way to do it?
Thank you in advance!
You can make use of the \K meta escape since PCRE doesn't support variable width lookbehinds:
regex:
[0-9]+\K[\.](?=[0-9]+[,][0-9]+)
[0-9]+ - capture digits
\K - forget what we've captured
[\.] - capture a period; just \. can be used, no need for the char class brackets
(?=[0-9]+[,][0-9]+) - ahead of me should be digits followed by a comma and digits
replace:
Nothing
\K is bugged in Notepad++ so you could use this regex instead since you only care that at least one digit is behind the period:
(?<=\d)\.(?=[0-9]+[,][0-9]+)
You can use \K, which basically says throw away whatever was matched up until that point, then add a lookahead. Like so
[0-9]+\K\.(?=[0-9]+[,][0-9]+)
Change the regular expression to: ([0-9]+)[\.]([0-9]+[,][0-9]+)
The () pieces are groups which you can refer to in the replace with \1 for the first group, and \2 for the second group.
The docs also explain this here: https://npp-user-manual.org/docs/searching/#substitution-grouping (even better, and in more detail, than my usage in this answer...)
EDIT: I just wanted to share the animated gif showing that 'Replace' in Notepad++ 7.9.5. does not seem to work.

Regex positive look behind notepad++

I'm trying to use regex to grab the last 3 digits of a string that varies in size. The beginning of the string always leads with name-
Examples:
"Name-0000425"
"Name-00123"
"Name-123"
I want the regex to grab the last 3 digits of this number and replace it with these digits. This is the regex I have tried but it only grabs the first 3 digits.
(?-s)(?<=Name-)(\d{3})
Any help would be appreciated! Thanks!
You can use
(?<=Name-)\d*(\d{3})
Replace with $1. See the regex demo.
Alternatively, you may also use (Name-)\d*(\d{3}) and replace with $1$2.
Details
(?<=Name-) - a positive lookbehind that matches a location immediately preceded with Name-
\d* - any zero or more digits
(\d{3}) - Group 1 ($1 refers to this value from the replacement pattern): three digits.
NOTES
If you only want to remove initial zeros, you can use the (?<=Name-)0+(\d{3}) regex.
If there can be anything else before the last three digits on the line, you may use (?<=Name-).*(\d{3}).
If there can be anything else before the last three digits that are inside double quotes on a single line, you may use (?<=Name-)[^"\n\r]*(\d{3}).
Shortest regex will be...
Find what: \d{3}"
Replace it with 789"

RegEx for adding a zero between a dash and number [duplicate]

This question already has answers here:
Replacing digits immediately after a saved pattern
(2 answers)
Closed 3 years ago.
I want to find a way to add a leading zero "0" in front of numbers but BBEdit thinks it's substitute #10 Example:
Original string: Video 2-1: Title Goes Here
Desired result: Video 2-01: Title Goes Here
My find regex is: (-)(\d:)
My replace regex is: \10\2. The first substitute is NOT 10. I simply intend to replace first postion, then add a "0", then replace second position.
Kindly tell me how to tell BBEdit that I want to add a zero and that I don't mean 10th position.
If you simply need a number preceded by a dash, then I recommend using the regex lookbehind for this one.
Try this out:
(?<=-)(\d+:)
As seen here: regex101.com
It tells the regex that the match should be preceded by a dash -, and the - itself won't be matched!
You really don't need to capture hyphen in group1 (as it is a fixed string so no benefit capturing in group1 and replacing with \1) for replacement, instead just capture hyphen with digit using -(\d+:) and while replacing just use -0\1
Regex Demo
Also, there are other better ways to make the replacement where you don't need to deal with back references at all.
Another alternate solution is to use this look around based regex,
(?<=-)(?=\d+:)
and replace it with just 0 which will just insert a zero before the digit.
Regex Demo with lookaround
Another alternate solution when lookbehind is not supported (like in Javascript prior to EcmaScript2018), you can use a positive look ahead based solution. Basically match a hyphen - which is followed by digits and colon using this regex,
-(?=\d+:)
and replace it with -0
Regex Demo with only positive look ahead
Try \1\x30\2 as the replacement. \x30 is the hex escape for the 0 character, so the replacement is \1, then 0, then \2, and cannot be interpreted as \10 then 2. I don't know if BBEdit supports hex escapes in the replacement string though.
This expression might help you to do so, if Video 2- is a fixed input:
(Video 2-)(.+)
If you have other instances, you can add left boundary to this expression, maybe something similar to this:
([A-Za-z]+\s[0-9]+-)(.+)
Then, you can simply replace it with a leading zero after capturing group $1:
Graph
This graph shows how the expression would work:
If you wish, you can add additional boundaries to the expression.
Replacement
For replacing, you can simply use \U0030 or \x30 instead of zero, whichever your program might support, in between $1 and $2.

Replacing digits immediately after a saved pattern

Searched pattern looks like text9
I search for (text)9
I want to replace with \15 so that I would get text5 but instead it's just giving me text.
Any other character works except for digits.
As it turns out, the PCRE-style back-references do not work.
So, you have to use \015 to replace with the text captured with the first capturing group (\01) and 5.
Since there cannot be more than 99 capturing groups, and both the digits after \ are treated as back-reference group number, \01 is interpreted as the reference to the first group, and the rest are literal digits.
The replacement term \15 is being interpreted as "group 15" - you must escape the "5":
Try replacing with \1\\5, or if that doesn't work (I don't have textwrangler handy) use a look behind:
Search: (?<=text)9
Replace: 5
The look behind doesn't consume input, so only the "9" is matched.

How to replace only part of found text?

I have a file with a some comma separated names and some comma separated account numbers.
Names will always be something like Dow, John and numbers like 012394,19862.
Using Notepad++'s "Regex Find" feature, I'd like to replace commas between numbers with pipes |.
Basically :
turn: Dow,John into: Dow,John
12345,09876 12345|09876
13568,08642 13568|08642
I've been using [0-9], to find the commas, but I can't get it to properly leave the number's last digit and replace just the comma.
Any ideas?
Search for ([0-9]), and replace it with \1|. Does that work?
use this regex
(\d),(\d)
and replace it with
$1|$2
OR
\1|\2
(?<=\d), should work. Oddly enough, this only works if I use replace all, but not if I use replace single. As an alternative, you can use (\d), and replace with $1|
General thoughts about replacing only part of a match
In order to replace a part of a match, you need to either 1) use capturing groups in the regex pattern and backreferences to the kept group values in the replacement pattern, or 2) lookarounds, or 3) a \K operator to discard left-hand context.
So, if you have a string like a = 10, and you want to replace the number after a = with, say, 500, you can
find (a =)\d+ and replace with \1500 / ${1}500 (if you use $n backreference syntax and it is followed with a digit, you should wrap it with braces)
find (?<=a =)\d+ and replace with 500 (since (?<=...) is a non-consuming positive lookbehind pattern and the text it matches is not added to the match value, and hence is not replaced)
find a =\K\d+ and replace with 500 (where \K makes the regex engine "forget" the text is has matched up to the \K position, making it similar to the lookbehind solution, but allowing any quantifiers, e.g. a\h*=\K\d+ will match a = even if there are any zero or more horizontal whitespaces between a and =).
Current problem solution
In order to replace any comma in between two digits, you should use lookarounds:
Find What: (?<=\d),(?=\d)
Replace With: |
Details:
(?<=\d) - a positive lookbehind that requires a digit immediately to the left of the current location
, - a comma
(?=\d) - a positive lookahead that requires a digit immediately to the right of the current location.
See the demo screenshot with settings:
See the regex demo.
Variations:
Find What: (\d),(?=\d)
Replace With: \1|
Find What: \d\K,(?=\d)
Replace With: |
Note: if there are comma-separated single digits, e.g. 1,2,3,4 you can't use (\d),(\d) since this will only match odd occurrences (see what I mean).