java 用正则表达式用双引号替换单引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17009265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replace single quote with double quote with Regex
提问by quarks
I have an app that received a malformed JSON string like this:
我有一个应用程序收到了一个格式错误的 JSON 字符串,如下所示:
{'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
}
- a 后面
{
有一个或多个空格的单引号 - 出现在一个或多个空格之前
:
- 在 a 之后
:
还有一个空格 - 出现在一个或多个空格之前
}
So this String {'username' : 'xirby'}
or
所以这个字符串{'username' : 'xirby'}
或
{ 'username' : 'xirby' }
Would be transformed to:
将转换为:
{"username" : "xirby"}
Update:
更新:
Also a possible malformed JSON String:
还有一个可能的格式错误的 JSON 字符串:
{ 'message' : 'there's not much to say' }
In this example the single quote inside the message value should not be replaced.
在此示例中,不应替换消息值中的单引号。
采纳答案by NeverHopeless
回答by Derek Chang
This regex will capture all appropriate single quotes and associated white spaces while ignoring single quotes inside a message. One can replace the captured characters with double quotes, while preserving the JSON format. It also generalizes to JSON strings with multiple messages (delimited by commas ,
).
此正则表达式将捕获所有适当的单引号和相关的空格,同时忽略消息中的单引号。可以用双引号替换捕获的字符,同时保留 JSON 格式。它还可以推广到具有多条消息(以逗号分隔,
)的JSON 字符串。
((?<={)\s*\'|(?<=,)\s*\'|\'\s*(?=:)|(?<=:)\s*\'|\'\s*(?=,)|\'\s*(?=}))
I know you tagged your question for java, but I'm more familiar with python. Here's an example of how you can replace the single quotes with double quotes in python:
我知道你为 java 标记了你的问题,但我更熟悉 python。这是一个如何在python中用双引号替换单引号的示例:
import re
regex = re.compile('((?<={)\s*\'|(?<=,)\s*\'|\'\s*(?=:)|(?<=:)\s*\'|\'\s*(?=,)|\'\s*(?=}))')
s = "{ 'first_name' : 'Shaquille' , 'lastname' : 'O'Neal' }"
regex.sub('"', s)
> '{"first_name":"Shaquille","lastname":"O\'Neal"}'
This method looks for single quotes next to the symbols {},:
using look-ahead and look-behind operations.
此方法{},:
使用前瞻和后视操作查找符号旁边的单引号。
回答by fge
Instead of doing this, you're better off using a JSON parser which can read such malformed JSON and "normalize" it for you. Hymansoncan do that:
与其这样做,不如使用 JSON 解析器,它可以读取这种格式错误的 JSON 并为您“规范化”它。Hyman逊可以这样做:
final ObjectReader reader = new ObjectMapper()
.configure(Feature.ALLOW_SINGLE_QUOTES, true)
.reader();
final JsonNode node = reader.readTree(yourMalformedJson);
// node.toString() does the right thing
回答by flavian
String test = "{'username' : 'xirby'}";
String replaced = test.replaceAll("'", "\"");
回答by Malus Jan
Concerning your question's tag is JAVA, I answered in JAVA.
关于你的问题的标签是JAVA,我在JAVA 中回答。
At first import the libraries:
首先导入库:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Then:
然后:
Pattern p = Pattern.compile("((?<=(\{|\[|\,|:))\s*')|('\s*(?=(\}|(\])|(\,|:))))");
String s = "{ 'firstName' : 'Malus' , 'lastName' : ' Ms'Malus' , marks:[ ' A+ ', 'B+']}";
String replace = "\"";
String o;
Matcher m = p.matcher(s);
o = m.replaceAll(replace);
System.out.println(o);
Output:
输出:
{"firstName":"Malus","lastName":" Ms'Malus", marks:[" A+ ","B+"]}
回答by Charles Marsh
If you're looking to exactly satisfy all of those conditions, try this:
如果您希望完全满足所有这些条件,请尝试以下操作:
'{(\s)?\'(.*)\'(\s)?:(\s)?\'(.*)\'(\s)?}'
as you regex. It uses (\s)? to match one or zero whitespace characters.
正如你正则表达式。它使用 (\s)? 匹配一个或零个空白字符。
回答by Sarath Kumar Sivan
I recommend you to use a JSON parser instead of REGEX.
我建议您使用 JSON 解析器而不是 REGEX。
String strJson = "{ 'username' : 'xirby' }";
strJson = new JSONObject(strJson).toString();
System.out.println(strJson);