5

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?

5
  • 1
    Possible duplicate of Use a function in Powershell replace Commented Feb 8, 2016 at 14:35
  • @PetSerAl: Why? It can be easily solved with a single regex. Commented Feb 8, 2016 at 14:36
  • @WiktorStribiżew Given the OP code, I think it what he trying to do, so I vote for it. Commented Feb 8, 2016 at 14:58
  • Yeah, true. I will add a note about that in my answer. Commented Feb 8, 2016 at 14:59
  • Thanks for all the answers, I could solve my problem with this. Commented Feb 8, 2016 at 16:38

3 Answers 3

7

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

Sign up to request clarification or add additional context in comments.

1 Comment

:) Good choice of the alternatives :) Yours seems the best suggestion.
4

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.

Comments

3

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.

1 Comment

@Shihan: bobble bubble's suggestion is the most efficient although all regexps here are working as you expect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.