用Regex替换双引号的单引号 [英] Replace single quote with double quote with Regex
本文介绍了用Regex替换双引号的单引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个应用程序收到格式错误的JSON字符串,如下所示:
I have an app that received a malformed JSON string like this:
{'username' : 'xirby'}
我需要替换单引号'
双重摊位
I need to replaced the single quotes '
with double quoates "
根据这些规则(我认为):
With these rule (I think):
- 单个报价来自
{
带有一个或多个空格 - 来自一个或更多空格和
:
- 再来一个
:
还有一个空格 - 来自一个或多个空格并且
}
- A single quote comes after a
{
with one or more spaces - Comes before one or more spaces and
:
- Comes after a
:
with one more spaces - Comes before one or more spaces and
}
所以这个字符串 {'用户名':'xirby'}
或
{ 'username' : 'xirby' }
将转换为:
{"username" : "xirby"}
更新:
也可能是格式错误的JSON字符串:
Also a possible malformed JSON String:
{ 'message' : 'there's not much to say' }
在这个例子中,消息值中的单引号不应该被替换。
In this example the single quote inside the message value should not be replaced.
推荐答案
试试这个正则表达式:
\s*\'\s*
并调用替换为将完成这项工作。看看此处。
and a call to Replace with "
will do the job. Look at here.
这篇关于用Regex替换双引号的单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文