【问题标题】:Regex to remove `.` from a sub-string enclosed in square brackets正则表达式从方括号中的子字符串中删除 `.`
【发布时间】:2015-06-22 08:40:40
【问题描述】:

我在 C# 中有这个正则表达式:

\[.+?\]

此正则表达式提取方括号之间的子字符串。但在此之前,我想删除这些子字符串中的.。例如,字符串

hello,[how are yo.u?]There are [300.2] billion stars in [Milkyw.?ay].

应该变成

hello,[how are you?]There are [3002] billion stars in [Milkyw?ay].

我不擅长形成正则表达式,所以不知道如何修改我的正则表达式。

此处的示例:https://regex101.com/r/pL5uA1/1

【问题讨论】:

  • 为什么? 被删除了?
  • @AvinashRaj 哦,对不起,我错过了。 :)
  • 你确定你的反斜杠是对的吗?当你用另一种语言编写正则表达式时,你需要对它进行双重转义。
  • @sqd:C# 具有原始字符串语法。

标签: c# .net regex string replace


【解决方案1】:

删除方括号内的所有点。

Regex.Replace(str, @"\.(?=[^\[\]]*\])", "");

DEMO

删除点或?

Regex.Replace(str, @"[.?](?=[^\[\]]*\])", "");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-07
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    • 2011-11-07
    • 1970-01-01
    相关资源
    最近更新 更多