【发布时间】:2012-11-13 04:10:03
【问题描述】:
我有以下字符串数据:
data = "*****''[[dogs and cats]]''/n"
我想在 python 中使用正则表达式来提取字符串。所有数据都包含在双引号“”中。我使用了哪些通配符,以便获得以下信息:
print data.groups(1)
print data.groups(2)
print data.groups(3)
'dogs'
'and'
'cats'
编辑:到目前为止,我有一些很长的内容
test = re.search("\\S*****''[[(.+) (.+) (.+)\\S]]''", "*****''[[dogs and cats]]''\n")
print test.group(1)
【问题讨论】:
-
你有没有尝试过自己解决这个问题?
-
你说你想要的数据是双引号,但你的文本中唯一的双引号是在 python 字符串周围。你的意思是两个单引号
''(而不是双引号")?是否应该始终忽略非字母字符(例如方括号)? -
另外,您可能希望接受之前问题的答案,以鼓励人们为您提供更多帮助!
-
感谢您的意见。我仍在学习 StackOverflow 的诀窍。是的,@Austin Henley,我今天已经为此工作了一段时间。到目前为止,我的 thistest = re.search("\\S*****''[[(.+) (.+) (.+)\\S]]''", "*****''[[dogs and cats]]''\n") print test.group(1)
-
您应该将该代码放入问题本身(您也可以在那里正确格式化它!)。当您使用该代码时会发生什么?您希望发生什么不同的事情?
标签: python regex string pattern-matching wildcard