替换没有被单引号包围的双引号 [英] Replace double quotes not surrounded by single quotes

查看:58
本文介绍了替换没有被单引号包围的双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果双引号没有被使用 PCRE 的单引号包围,我想用单引号替换双引号.一些例子,意思是输入=>输出

I want to replace double quotes with single quotes if they aren't surrounded by single qoutes using PCRE. Some examples, the meaning is input => output

"foo" => 'foo'
'foo' => 'foo'
abc "foo" => abc 'foo'
foo "bar", "baz" => foo 'bar', 'baz'
abc 'foo "bar" baz' => abc 'foo "bar" baz'

我尝试了 ^([^'"]*)"([^'"]*)" 与修饰符 m multi-line 和 'g global`.只要每行不超过一个xyz"块(在第 4 行失败),这种方法就有效.

I tried ^([^'"]*)"([^'"]*)" with the modifiers m multi-line and 'g global`. This works as far as there isn't more than one "xyz" block per line (fails in line 4).

我忘了提到我目前的替代品是 $1'$2'

I forgot to mention that my current substitution is $1'$2'

推荐答案

下面的解决方案仅适用于输入字符串没有转义序列的情况:

/'[^']*'(*SKIP)(*F)|"/

查看正则表达式演示

  • '[^']*' - match a single quote, 0+ chars other than a single quote and a single quote again
  • (*SKIP)(*F) - PCRE verb sequence discarding the match and proceeding from the current index (you can read more on that at How do (*SKIP) or (*F) work on regex? SO post)
  • | - or...
  • " - a double quote.

如果有转义序列,你需要写一个解析器.

这篇关于替换没有被单引号包围的双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆