用不同的字符替换字符串中的多个点但数量相同

时间:2021-12-17 08:24:18

I have a string like the following "blaa...blup..blaaa...bla."

我有一个像下面的字符串“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."

字符串应该导致:“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:

我尝试在powershell中使用以下正则表达式方法,但无论是否有3个或更多点,我总是得到匹配的第2个:

$string -replace '(.)\1+',("_"*'$&'.length)

Any ideas?

3 个解决方案

#1


7  

You can use the \G anchor to glue a match to the previous.

您可以使用\ G锚点将匹配粘贴到上一个。

\.(?=\.)|\G(?!^)\.
  • \.(?=\.) match a period if another one is ahead.
  • \。(?= \。)匹配一个句号,如果另一个领先。

  • |\G(?!^)\. or replace period if there was a previous match (but not start)
  • | \ G□\(^!)。或者如果之前有匹配(但不是开始)则替换句点

Replace with underscore. See demo at regexstorm

替换为下划线。请参阅regexstorm上的演示

#2


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.

我所知道的语言和正则表达式都不允许您“动态”评估反向引用数值,您只能在回调函数中使用它。请参阅在Powershell替换中使用功能。

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)
  • ((?= \。{2})|(?!^)\ G) - 与2点之前的位置匹配的边界(带(?= \。{2}))或上一次成功匹配的结束(用(?!^)\ G)

  • \. - a literal dot.
  • \。 - 一个字面点。

#3


3  

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.

请参阅在线演示。

#1


7  

You can use the \G anchor to glue a match to the previous.

您可以使用\ G锚点将匹配粘贴到上一个。

\.(?=\.)|\G(?!^)\.
  • \.(?=\.) match a period if another one is ahead.
  • \。(?= \。)匹配一个句号,如果另一个领先。

  • |\G(?!^)\. or replace period if there was a previous match (but not start)
  • | \ G□\(^!)。或者如果之前有匹配(但不是开始)则替换句点

Replace with underscore. See demo at regexstorm

替换为下划线。请参阅regexstorm上的演示

#2


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.

我所知道的语言和正则表达式都不允许您“动态”评估反向引用数值,您只能在回调函数中使用它。请参阅在Powershell替换中使用功能。

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)
  • ((?= \。{2})|(?!^)\ G) - 与2点之前的位置匹配的边界(带(?= \。{2}))或上一次成功匹配的结束(用(?!^)\ G)

  • \. - a literal dot.
  • \。 - 一个字面点。

#3


3  

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.

请参阅在线演示。