除 ']]' 字符外的任何正则表达式
Regular expression for anything except ']]' characters
我之前发过一个问题,但不是很清楚,所以这里再说一遍:
我有一个字符串,如下所示:
{
"1000":[ [some whitespace and nonwhitespace characters],
[some whitespace and nonwhitespace characters],
....
[some whitespace and nonwhitespace characters]],
"1001":[ [some whitespace and nonwhitespace characters],
[some whitespace and nonwhitespace characters],
....
[some whitespace and nonwhitespace characters]],
...
}
我想使用正则表达式提取如下所示的记录:
"1000":[ [some whitespace and nonwhitespace characters],
[some whitespace and nonwhitespace characters],
....
[some whitespace and nonwhitespace characters]]
我在 python 中使用 re 模块
为此,我想到了模式:
' "[0-9]{4}":(anything except ]] ) '
但我无法弄清楚 除了 ']]'
之外的任何内容的模式是什么
有人能帮忙吗?
正则表达式解决方案可以使用类似的东西来实现:
\d{4}":(.*?)]]
但是如果您的字符串是有效的 JSON,您真的不想在这里使用正则表达式。 Python 与 JSON 一起工作是很自然的。假设您的数据是:
data = {key1: [[str1], [str2], ...], ...}
您可以通过访问相应的键来简单地获取key1
的值:
data[key1]
这会给你:
我之前发过一个问题,但不是很清楚,所以这里再说一遍:
我有一个字符串,如下所示:
{
"1000":[ [some whitespace and nonwhitespace characters],
[some whitespace and nonwhitespace characters],
....
[some whitespace and nonwhitespace characters]],
"1001":[ [some whitespace and nonwhitespace characters],
[some whitespace and nonwhitespace characters],
....
[some whitespace and nonwhitespace characters]],
...
}
我想使用正则表达式提取如下所示的记录:
"1000":[ [some whitespace and nonwhitespace characters],
[some whitespace and nonwhitespace characters],
....
[some whitespace and nonwhitespace characters]]
我在 python 中使用 re 模块
为此,我想到了模式:
' "[0-9]{4}":(anything except ]] ) '
但我无法弄清楚 除了 ']]'
之外的任何内容的模式是什么有人能帮忙吗?
正则表达式解决方案可以使用类似的东西来实现:
\d{4}":(.*?)]]
但是如果您的字符串是有效的 JSON,您真的不想在这里使用正则表达式。 Python 与 JSON 一起工作是很自然的。假设您的数据是:
data = {key1: [[str1], [str2], ...], ...}
您可以通过访问相应的键来简单地获取key1
的值:
data[key1]
这会给你: