Shihan February 2016

Replace multiple dots in string with different character but same amount

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?

Answers


Wiktor Stribi┼╝ew February 2016

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.

enter image description 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.


Steven Doggart February 2016

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 period

See the online demo.


bobble bubble February 2016

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

Post Status

Asked in February 2016
Viewed 2,202 times
Voted 5
Answered 3 times

Search




Leave an answer