Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 803
Patric Avatar asked Feb 08 '16 14:02

Patric


3 Answers

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

like image 63
bobble bubble Avatar answered Nov 17 '22 07:11

bobble bubble


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.

like image 30
Steven Doggart Avatar answered Nov 17 '22 07:11

Steven Doggart


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.
like image 3
Wiktor Stribiżew Avatar answered Nov 17 '22 07:11

Wiktor Stribiżew