如何使用正则表达式替换标签内 csv 的双引号
How to replace double quotes from csv inside a tag using Regex
我有一个 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>"
这里有一个方法:
- Ctrl+H
- 查找内容:
(?:<Note>|\G(?!^))(?:(?!</Note)[^"])*\K"(?=.*</Note>)
- 替换为:
LEAVE EMPTY
- 检查 匹配大小写
- 检查 环绕
- 检查 正则表达式
- 取消选中
. 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
截图(之前):
截图(后):
我有一个 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>"
这里有一个方法:
- Ctrl+H
- 查找内容:
(?:<Note>|\G(?!^))(?:(?!</Note)[^"])*\K"(?=.*</Note>)
- 替换为:
LEAVE EMPTY
- 检查 匹配大小写
- 检查 环绕
- 检查 正则表达式
- 取消选中
. 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
截图(之前):
截图(后):