I have a string like the following "blaa...blup..blaaa...bla."
Every part where there is more than one dot must be replaced by "_" but it must have the same amount as replaced chars.
The string should result in: "bla___blup__blaaa___bla."
Notice that the last dot is not replaced as it has not other dots "connected".
I tried using following regex approach in powershell but I always get a legnth of 2 for the match regardless if there where 3 or more dots:
$string -replace '(.)\1+',("_"*'$&'.length)
Any ideas?
You can use the \G anchor to glue a match to the previous.
\.(?=\.)|\G(?!^)\.
\.(?=\.)
match a period if another one is ahead.|\G(?!^)\.
or replace period if there was a previous match (but not start)Replace with underscore. See demo at regexstorm
You can use the following pattern:
\.(?=\.)|(?<=\.)\.
And replace with _
.
The pattern simply looks for either a period that is preceded by a period or a period which is followed by a period:
\.(?=\.)
- Matches a period which is followed by a period|
- Or(?<=\.)\.
- Matches a period which is preceded by a periodSee the online demo.
None of the languages and regex flavors I know allow you to evaluate the backreference numeric value "on the fly", you can only use it in the callback function. See Use a function in Powershell replace.
However, in this particular case, you can use the following regex:
((?=\.{2})|(?!^)\G)\.
And replace with _
.
See the regex demo here.
And the explanation:
((?=\.{2})|(?!^)\G)
- a boundary that either matches a location before 2 dots (with (?=\.{2})
) or the end of the previous successful match (with (?!^)\G
)\.
- a literal dot.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