I want a python regex to capture either a bracket or an empty string. Trying the usual approach is not working. I need to escape something somewhere but I've tried everything I know.
我想要一个python正则表达式来捕获括号或空字符串。尝试通常的方法是行不通的。我需要在某处逃避某些事情,但我已经尝试了所有我知道的事情。
one = "this is the first string [with brackets]"
two = "this is the second string without brackets"
# This captures the bracket on the first but throws
# an exception on the second because no group(1) was captured
re.search('(\[)', one).group(1)
re.search('(\[)', two).group(1)
# Adding a "?" for match zero or one occurrence ends up capturing an
# empty string on both
re.search('(\[?)', one).group(1)
re.search('(\[?)', two).group(1)
# Also tried this but same behavior
re.search('([[])', one).group(1)
re.search('([[])', two).group(1)
# This one replicates the first solution's behavior
re.search("(\[+?)", one).group(1) # captures the bracket
re.search("(\[+?)", two).group(1) # throws exception
Is the only solution for me to check that the search returned None?
是我唯一的解决方案,检查搜索返回无?
3 个解决方案
#1
6
The answer is simple! :
答案很简单! :
(\[+|$)
Because the only empty string you need to capture is the last of the string.
因为您需要捕获的唯一空字符串是字符串的最后一个。
#2
2
Here's a different approach.
这是一种不同的方法。
import re
def ismatch(match):
return '' if match is None else match.group()
one = 'this is the first string [with brackets]'
two = 'this is the second string without brackets'
ismatch(re.search('\[', one)) # Returns the bracket '['
ismatch(re.search('\[', two)) # Returns empty string ''
#3
0
Ultimately, the thing I wanted to do is to take a string and, if I find any square or curly brackets, remove the brackets and their contents from the string. I was trying to isolate the strings that needed fixing first by finding a match and the fixing the resulting list in a second step when all I needed to do was do both at the same time as follows:
最终,我想要做的是取一个字符串,如果我找到任何方括号或大括号,从字符串中删除括号及其内容。我试图通过查找匹配来确定首先需要修复的字符串,并在第二步修复结果列表时,我需要做的就是同时执行以下操作:
re.sub ("\[.*\]|\{.*\}", "", one)
#1
6
The answer is simple! :
答案很简单! :
(\[+|$)
Because the only empty string you need to capture is the last of the string.
因为您需要捕获的唯一空字符串是字符串的最后一个。
#2
2
Here's a different approach.
这是一种不同的方法。
import re
def ismatch(match):
return '' if match is None else match.group()
one = 'this is the first string [with brackets]'
two = 'this is the second string without brackets'
ismatch(re.search('\[', one)) # Returns the bracket '['
ismatch(re.search('\[', two)) # Returns empty string ''
#3
0
Ultimately, the thing I wanted to do is to take a string and, if I find any square or curly brackets, remove the brackets and their contents from the string. I was trying to isolate the strings that needed fixing first by finding a match and the fixing the resulting list in a second step when all I needed to do was do both at the same time as follows:
最终,我想要做的是取一个字符串,如果我找到任何方括号或大括号,从字符串中删除括号及其内容。我试图通过查找匹配来确定首先需要修复的字符串,并在第二步修复结果列表时,我需要做的就是同时执行以下操作:
re.sub ("\[.*\]|\{.*\}", "", one)