替换正则表达式中引号内的值,如果只有引号则忽略。

huangapple go评论62阅读模式
英文:

Regex: Replace with value inside quotation marks and ignore if just quotation marks

问题

test dd,test,"",test,tes_d

英文:

I have this:

"test dd","test","","test","tes_d"

I would like this:

test dd,test,"",test,tes_d

It works with "([^"]*)", but it replaces third value ("") with empty string.

答案1

得分: 1

你可以使用以下正则表达式:

("")|"([^"]+)"

替换为:

$1$2

详细信息:

  • ("") - 第一组 ($1):"" 字符串
  • | - 或
  • "([^"]+)" - 一个 ",然后任意一个或多个不是 " 的字符(捕获到第二组,$2),然后是一个 "
英文:

You can use

("")|"([^"]+)"

Replace with $1$2, see the regex demo.

Details

  • ("") - Group 1 ($1): "" string
  • | - or
  • "([^"]+)" - a ", then any one or more chars other than " (captured into Group 2, $2) and then a ".

huangapple
  • 本文由 发表于 2023年3月3日 21:55:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627978.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定