简体   繁体   English

正则表达式在字符串中查找括号

[英]Regular Expression to find brackets in a string

I have a string which has multiple brackets. 我有一个有多个括号的字符串。 Let says 我们说吧

s="(a(vdwvndw){}]"

I want to extract all the brackets as a separate string. 我想将所有括号提取为单独的字符串。

I tried this: 我试过这个:

>>> brackets=re.search(r"[(){}[]]+",s)
>>> brackets.group()

But it is only giving me last two brackets. 但它只给了我最后两个括号。

'}]'

Why is that? 这是为什么? Shouldn't it fetch one or more of any of the brackets in the character set? 它不应该获取字符集中的任何一个或多个括号吗?

You have to escape the first closing square bracket. 你必须逃脱第一个关闭方括号。

r'[(){}[\]]+'

To combine all of them into a string, you can search for anything that doesn't match and remove it. 要将所有这些组合成一个字符串,您可以搜索任何匹配的内容并将其删除。

brackets = re.sub( r'[^(){}[\]]', '', s)

Use the following ( Closing square bracket must be escaped inside character class ): 使用以下( 关闭方括号必须在字符类转义 ):

brackets=re.search(r"[(){}[\]]+",s)
                           ↑

The regular expression "[(){}[]]+" (or rather "[](){}[]+" or "[(){}[\\]]+" (as others have suggested)) finds a sequence of consecutive characters. 正则表达式"[(){}[]]+" (或更确切地说是"[](){}[]+""[(){}[\\]]+" (正如其他人建议的那样))找到一个连续字符序列。 What you need to do is find all of these sequences and join them. 您需要做的是找到所有这些序列并加入它们。

One solution is this: 一个解决方案是:

brackets = ''.join(re.findall(r"[](){}[]+",s))

Note also that I rearranged the order of characters in a class, as ] has to be at the beginning of a class so that it is not interpreted as the end of class definition. 还需要注意的是我重新排列字符的顺序在一个类中,如]必须是在类的开始,使之不被解释为类定义的结束。

You could also do this without a regex: 你也可以在没有正则表达式的情况下做到这一点:

s="(a(vdwvndw){}]"
keep = {"(",")","[","]","{","}"}
print("".join([ch for ch in s if ch in keep]))
((){}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM