I have the following example of numbers, and I need to add a zero after the second period (.).
1.01.1
1.01.2
1.01.3
1.02.1
I would like them to be:
1.01.01
1.01.02
1.01.03
1.02.01
I have the following so far:
Search:
^([^.])(?:[^.]*\.){2}([^.].*)
Substitution:
0\1
but this returns:
01 only.
I need the 1.01. to be captured in a group as well, but now I'm getting confuddled.
Does anyone know what I am missing?
Thanks!!
You may try this regex replacement with 2 capture groups:
Search:
^(\d+\.\d+)\.([1-9])
Replacement:
\1.0\2
RegEx Demo
RegEx Details:
^
: Start(\d+\.\d+)
: Match 1+ digits + dot followed by 1+ digits in capture group #1\.
: Match a dot([1-9])
: Match digits 1-9
in capture group #2 (this is to avoid putting 0
before already existing 0
)\1.0\2
inserts 0
just before capture group #2You could try:
^([^.]*\.){2}\K
Replace with 0
. See an online demo
^
- Start line anchor.([^.]*\.){2}
- Negated character 0+ times (greedy) followed by a literal dot, matched twice.\K
- Reset starting point of reported match.EDIT:
Or/And if \K
meta escape isn't supported, than see if the following does work:
^((?:[^.]*\.){2})
Replace with ${1}0
. See the online demo
^
- Start line anchor.(
- Open 1st capture group;
(?:
- Open non-capture group;
){2}
- Close non-capture group and match twice.)
- Close capture group.Using your pattern, you can use 2 capture groups and prepend the second group with a dot in the replacement like for example \g<1>0\g<2>
or ${1}0${2}
or $10$2
depending on the language.
^((?:[^.]*\.){2})([^.])
^
Start of string((?:[^.]*\.){2})
Capture group 1, match 2 times any char except a dot, then match the dot([^.].*)
Capture group 2, match any char except a dotRegex demo
A more specific pattern could be matching the digits
^(\d+\.\d+\.)(\d)
^
Start of string(\d+\.\d+\.)
Capture group 1, match 2 times 1+ digits and a dot(\d)
Capture group 2, match a digitRegex demo
For example in JavaScript
const regex = /^(\d+\.\d+\.)(\d)/;
[
"1.01.1",
"1.01.2",
"1.01.3",
"1.02.1",
].forEach(s => console.log(s.replace(regex, "$10$2")));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With