【问题标题】:replace all commas in quotes with regex in notepad++用记事本++中的正则表达式替换引号中的所有逗号
【发布时间】:2016-12-06 17:31:54
【问题描述】:

我正在尝试替换 notepad++ 中引号之间的字符串中的所有逗号。

我几乎用regex to remove comma between double quotes notepad++ 到达那里,但不完全是。就目前而言,它只是替换了第一个逗号。

总结这篇文章,它使用 Find What: ("[^",]+),([^"]+") Replace: \1\2 (我改为 \1~\2 )

在正则表达式中有没有办法捕获引号之间的所有逗号实例?

编辑:添加几个有代表性的字符串:

1,G 32,170696,01/06/2015,Jun-17,"M12 X 1,50 - 4H GO SRG",P U7,,,,SRG ,"G 32_170696_06-2017_M12 X 1,50 - 4H GO SRG_P U7.pdf"
3,13247,163090,01/11/2015,Nov-17,"PG 0,251 to 0,500 inch",P U7,,,,,"13247_163090_11-2017_PPG 0,251 to 0,500 inch_P U7.pdf"
9,PI 1496,182411,01/04/2015,Apr-17,"6,000 - 6,018mm GO-NOGO  PPG",,,,,PPG,"PI 1496_182411_04-2017_6,000 - 6,018mm GO-NOGO  PPG.pdf"

【问题讨论】:

  • 您只需点击全部替换几次,直到没有匹配。
  • @WiktorStribiżew:如果这样做,您还将替换引号外的逗号。
  • 您应该在您的问题中添加一个具有代表性的示例字符串。还可以尝试更好地格式化您的问题,请参阅:stackoverflow.com/editing-help

标签: regex notepad++


【解决方案1】:

您可以使用此模式一次性完成:

(?:\G(?!^)|([^"]*(?:"[^,"]*"[^"]*)*"))[^",]*\K,([^",]*+(?:"(?1)|$))?

使用此替换:~\2

demo

详情:

(?:
    \G(?!^) # contiguous to a previous match
  | # OR
    ([^"]*(?:"[^,"]*"[^"]*)*") # capture group 1: first match
                               # reach the first quoted part with commas
)
[^",]* \K ,  #"#
( # capture group 2: succeeds after the last comma
    [^",]*+  #"#
    (?:
        " (?1) #"# reach the next quoted part with commas
               # (using the capture group 1 subpattern)
      | # OR
        $ # end of the string: (this set a default behavior: when
          # the closing quote is missing)
    )
)?

【讨论】:

  • 哇,不错的正则表达式。不过,它似乎在最后一个 " 之后去掉了逗号。至少对我来说。我有点努力地遵循它,所以我自己没有设法修复它。我在这个数据上进行了测试:123|"Test,Test,Test", 456,789,"一,二" 246|"测试,测试,测试",456,789,一
猜你喜欢
  • 2011-09-17
  • 1970-01-01
  • 2015-10-05
  • 2016-11-13
  • 2017-01-05
  • 1970-01-01
  • 1970-01-01
  • 2014-06-06
  • 1970-01-01
相关资源
最近更新 更多