【问题标题】:How to replace double quotes from csv inside a tag using Regex如何使用正则表达式替换标签内 csv 的双引号
【发布时间】:2021-10-04 19:13:04
【问题描述】:

我有一个 csv 文件,它在标签中包含双引号并用双引号括起来。需要用其他字符替换标签内的双引号。 例如。

"id"|"Name"|"Note"
"1"|"Sam"|"<Note> This is "a" Sample </Note>"
"2"|"Sam1"|"<Note> This "is "a" Sam"ple "</Note>"

期望的输出

"id"|"Name"|"Note"
"1"|"Sam"|"<Note> This is a Sample </Note>"
"2"|"Sam1"|"<Note> This is a Sample </Note>"

【问题讨论】:

  • " 总是在一些&lt;tag&gt;...double quotes may be here...&lt;/tag&gt; 中吗?
  • 是的,只有一个标签是 ,其中包含双引号,它是一个文本字段,因此双引号可以在 标签中的任何位置。由于该字段以双引号开头,应该以双引号结尾,但在 标签内双引号导致问题破坏逻辑
  • 问题可以分2步解决。 第 1 步: 提取 &lt;Note&gt; 标记之间的所有内容。 第 2 步:" 替换为其他字符。这样的两步解决方案适合您吗?
  • 感谢您的回复,但 CSV 文件非常大,它包含 2 GB,所以如果一切都在一个步骤中会很好,我尝试了以下正则表达式,但它没有找到里面的所有双引号 标签。 "(?=[^)

标签: regex csv notepad++


【解决方案1】:

这里有一个方法:

  • Ctrl+H
  • 查找内容:(?:&lt;Note&gt;|\G(?!^))(?:(?!&lt;/Note)[^"])*\K"(?=.*&lt;/Note&gt;)
  • 替换为:LEAVE EMPTY
  • 检查 匹配大小写
  • 检查 环绕
  • CHECK 正则表达式
  • 取消选中 . matches newline
  • 全部替换

说明:

(?:                 # non capture group
    <Note>              # literally, open tag
  |                   # OR
    \G(?!^)             # restart from last match position except beginning of line
)                   # end group
(?:                 # non capture group
    (?!                 # negative lookahead, make sure we haven't after:
        </Note              # literally close tag
    )                   # end lookahead
    [^"]                # any character that is not a double quote    
)*                  # end group, may appear 0 or more times
\K                  # forget all we have seen until this position
"                   # double quote
(?=.*</Note>)       # positive lookahead, make sure we have close tag after

屏幕截图(之前):

截图(之后):

【讨论】:

  • @HajiRahmatullah:谢谢。
  • 你太棒了,你拯救了我的一天谢谢 - @Toto
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-12
  • 1970-01-01
  • 1970-01-01
  • 2012-09-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多