Python 除';]]之外的任何对象的正则表达式';人物

Python 除';]]之外的任何对象的正则表达式';人物,python,regex,regex-negation,Python,Regex,Regex Negation,我之前发布了一个问题,但理解起来不太清楚,所以这里再次出现: 我有一个字符串,看起来像这样: { "1000":[ [some whitespace and nonwhitespace characters], [some whitespace and nonwhitespace characters], .... [some whitespace and nonwhitespace characters]], "1001":[ [some

我之前发布了一个问题,但理解起来不太清楚,所以这里再次出现:

我有一个字符串,看起来像这样:

{
"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]],
...
}
我想使用regex提取一条如下所示的记录:

"1000":[ [some whitespace and nonwhitespace characters],
         [some whitespace and nonwhitespace characters],
             ....
         [some whitespace and nonwhitespace characters]]
我正在使用re模块在python中执行此操作

对于这一点,我的脑海中有一个模式:

' "[0-9]{4}":(anything except ]] ) '
但是我不知道除了“]]”之外的任何东西的模式是什么


有人能帮忙吗?

可以使用以下方法实现正则表达式解决方案:

\d{4}":(.*?)]]
但是如果您的字符串是有效的JSON,那么您真的不想在这里使用正则表达式。Python使用JSON是非常自然的。假设您的数据是:

data = {key1: [[str1], [str2], ...], ...}
您只需访问相应的键即可获取
key1
的值:

data[key1]

这将告诉您:

字符串来自哪里?它实际上是JSON吗?你能给我一个实际的样品吗?您是否考虑过“负面前瞻”?似乎将其解析为JSON并提取相关键比尝试使用正则表达式要好。不,问题在于找出非“]]”的表达式。我尝试了表达式“[0-9]{4}”:(?!]])“但不知怎么的,它不起作用。我怀疑你说的这句话是否正确:这不是什么,而是任何东西都能帮助@Maroun,谢谢,但问题是由于json文件太大,无法存储在单个对象中,而且用ijson解析也需要时间,所以我只在json文件的相关部分应用这个正则表达式来提取所需的数据。我认为正则表达式更像是
“\d{4}”:([\s\s]+?)]]
既然他说了任何包括换行符的话?@MoonCheesez这是真的。OP还可以通过
re.DOTALL