Python:如何打印正则表达式匹配的字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18761904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python: How to print a regex matched string?
提问by minerals
I want to match a part of the string (a particular word) and print it. Exactly what grep -odoes.
My word is "yellow dog" for example and it can be found in a string that spans over multiple lines.
我想匹配字符串的一部分(特定单词)并打印它。究竟是grep -o做什么的。例如,我的词是“yellow dog”,它可以在跨越多行的字符串中找到。
[34343] | ****. "Example": <one>, yellow dog
tstring0 123
tstring1 456
tstring2 789
Let's try this regex mydog = re.compile(', .*\n')and then
if mydog.search(string):print only the matched words.
让我们试试这个正则表达式mydog = re.compile(', .*\n'),然后
if mydog.search(string):只打印匹配的单词。
How do I get only "yellow dog" in the output?
如何在输出中只得到“黄狗”?
采纳答案by Chris Seymour
Using a capture group and findall:
使用捕获组和 findall:
>>> import re
>>> s = """[34343] | ****. "Example": <one>, yellow dog
... tstring0 123
... tstring1 456
... tstring2 789"""
>>> mydog = re.compile(', (.*)\n')
>>> mydog.findall(s)
['yellow dog']
If you only want the first match then:
如果您只想要第一场比赛,那么:
>>> mydog.findall(s)[0]
'yellow dog'
Note: you'd want to handle the IndexErrorfor when sdoesn't contain a match.
注意:您想要处理IndexErrorfor whens不包含匹配项。
回答by poke
If you don't specify a capture group, the text that is matched by the whole expression will be contained withing matchResult.group(0). In your case, this would be ', yellow dog\n'. If you just want the yellow dow, you should add a capture group to the expression: , (.*?)\n. Note that I also changed the .*into a .*?so that it will be non-greedy and stop when it finds the first line break.
如果不指定捕获组,则整个表达式匹配的文本将包含在matchResult.group(0). 在您的情况下,这将是', yellow dog\n'. 如果您只想要yellow dow,则应该向表达式中添加一个捕获组:, (.*?)\n。请注意,我还将 the 更改.*为 a ,.*?以便它不会贪婪并在找到第一个换行符时停止。
>>> s = '''[34343] | ****. "Example": <one>, yellow dog
tstring0 123
tstring1 456
tstring2 789'''
>>> mydog = re.compile(', (.*?)\n')
>>> matchResult = mydog.search(s)
>>> if matchResult:
print(matchResult.group(1))
yellow dog

