Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在使用正则表达式和双括号时遇到一些问题_Python_Regex_Nested - Fatal编程技术网

Python 在使用正则表达式和双括号时遇到一些问题

Python 在使用正则表达式和双括号时遇到一些问题,python,regex,nested,Python,Regex,Nested,我有一些使用正则表达式的经验,但我不能理解以下几点。如果我有一个字符串,比如: “[1,2,3],[3,5,3],[9,8,9]]aoeu[5,6,9]aoeu[[4,5,5]]” 什么正则表达式模式将分别提取[[1,2,3],[3,5,3],[9,8,9]和[[4,5,5]]?(封闭双括号内的任何组)。显然,“\[\[.\]\]”太贪婪了 如果输入完全遵循您的模式,那么您可以使用它使您的regex非贪婪 p = re.compile(ur'\[\[.*?\]\]') test_str = u"

我有一些使用正则表达式的经验,但我不能理解以下几点。如果我有一个字符串,比如:

“[1,2,3],[3,5,3],[9,8,9]]aoeu[5,6,9]aoeu[[4,5,5]]”


什么正则表达式模式将分别提取
[[1,2,3],[3,5,3],[9,8,9]
[[4,5,5]]
?(封闭双括号内的任何组)。显然,
“\[\[.\]\]”
太贪婪了

如果输入完全遵循您的模式,那么您可以使用它使您的
regex
非贪婪

p = re.compile(ur'\[\[.*?\]\]')
test_str = u"[[1,2,3],[3,5,3],[9,8,9]] aoeu [5,6,9] aoeu [[4,5,5]]"
print(re.findall(p, test_str))
要处理像
[[1,2,3],[3,5,3],3]
[1,2,3,[3,5,3],3]
等情况,请使用此正则表达式

(\[[^\[\]]*\[.*?\][^\]\[]*\])


如果你能使用Matthew Barnett的(更好的)魔法,你可以想出一些
\G
魔法:

分解并使用Python代码,这将是:

import regex as re

rx = re.compile(r"""
    (?:             # non capturing group
        (?:\[)      # an open bracket
        |           # OR
        (?!\A)\G    # make sure it's not the beginning... 
                    # ...and that it is the start of the last match
    )
    [^][]*          # not a [ or ]
    (\[[^]]+\])     # capture anything between two brackets
    """, re.VERBOSE)

string = '[[1,2,3],[3,5,3],[9,8,9]] aoeu [5,6,9] aoeu [[4,5,5]]'

matches = [match.group(1) for match in rx.finditer(string)]
print matches
# ['[1,2,3]', '[3,5,3]', '[9,8,9]', '[4,5,5]']

另外,请参见a。

使其非贪婪:-
\[\[.*.\]\]
检查:-如果输入是
'[[1,2,3],[3,5,3],3]bar[2,3]?
@AvinashRaj您的意思是使用“rock321987”中的正则表达式吗?@steveb我要求op进一步澄清。@AvinashRaj在您的问题中(对于模式
'[1,2,3],[3,5,3]。]3]bar[2,3]'
),rock321987
\[\[\[.*.\]\]\]
中的正则表达式将不匹配任何内容,因为
]
不存在。谢谢!这回答了我的问题。提到的第二个例子永远不会出现在我的例子中,因为它们在数学表达式中表示矩阵。该死,太晚了:)是的,我想我只是没有完全清楚/意识到懒惰和贪婪。我有一些阅读/练习要做。@Jan事实上,问题一出现,我就写了答案,但正在等待
op
的澄清。最后我把它贴了出来,因为
op
没有澄清从来没有用过
python
regex
库,但是
+1
永远从我这边来explanation@rock321987: 谢谢考虑一下,尽管它提供了一些特性“代码”> PCRE (<代码> > G/<代码>,<>代码(*Skip)(*FAILL)仅举几个)。当然,我会尝试一下。
import regex as re

rx = re.compile(r"""
    (?:             # non capturing group
        (?:\[)      # an open bracket
        |           # OR
        (?!\A)\G    # make sure it's not the beginning... 
                    # ...and that it is the start of the last match
    )
    [^][]*          # not a [ or ]
    (\[[^]]+\])     # capture anything between two brackets
    """, re.VERBOSE)

string = '[[1,2,3],[3,5,3],[9,8,9]] aoeu [5,6,9] aoeu [[4,5,5]]'

matches = [match.group(1) for match in rx.finditer(string)]
print matches
# ['[1,2,3]', '[3,5,3]', '[9,8,9]', '[4,5,5]']