我试图使用正则表达式来提取模式内的单词。

我有一些像这样的弦

someline abc
someother line
name my_user_name is valid
some more lines

我想提取单词my_user_name。我这样做

import re
s = #that big string
p = re.compile("name .* is valid", re.flags)
p.match(s)  # this gives me <_sre.SRE_Match object at 0x026B6838>

我现在如何提取my_user_name ?


当前回答

看起来您实际上是在试图提取一个名称,而不是简单地查找一个匹配项。如果是这种情况,为匹配设置span索引是有帮助的,我建议使用re.finditer。作为一个快捷方式,您知道正则表达式的名称部分的长度为5,有效的长度为9,因此您可以对匹配的文本进行切片以提取名称。

注意:在你的例子中,s看起来像是带换行符的字符串,所以这就是下面的假设。

## covert s to list of strings separated by line: s2 = s.splitlines() ## find matches by line: for i, j in enumerate(s2): matches = re.finditer("name (.*) is valid", j) ## ignore lines without a match if matches: ## loop through match group elements for k in matches: ## get text match_txt = k.group(0) ## get line span match_span = k.span(0) ## extract username my_user_name = match_txt[5:-9] ## compare with original text print(f'Extracted Username: {my_user_name} - found on line {i}') print('Match Text:', match_txt)

其他回答

这里有一种不使用组的方法(Python 3.6或以上):

>>> re.search('2\d\d\d[01]\d[0-3]\d', 'report_20191207.xml')[0]
'20191207'

看起来您实际上是在试图提取一个名称,而不是简单地查找一个匹配项。如果是这种情况,为匹配设置span索引是有帮助的,我建议使用re.finditer。作为一个快捷方式,您知道正则表达式的名称部分的长度为5,有效的长度为9,因此您可以对匹配的文本进行切片以提取名称。

注意:在你的例子中,s看起来像是带换行符的字符串,所以这就是下面的假设。

## covert s to list of strings separated by line: s2 = s.splitlines() ## find matches by line: for i, j in enumerate(s2): matches = re.finditer("name (.*) is valid", j) ## ignore lines without a match if matches: ## loop through match group elements for k in matches: ## get text match_txt = k.group(0) ## get line span match_span = k.span(0) ## extract username my_user_name = match_txt[5:-9] ## compare with original text print(f'Extracted Username: {my_user_name} - found on line {i}') print('Match Text:', match_txt)

也许这更简短,更容易理解:

import re
text = '... someline abc... someother line... name my_user_name is valid.. some more lines'
>>> re.search('name (.*) is valid', text).group(1)
'my_user_name'

您还可以使用捕获组(?P<user>模式)并像字典匹配['user']一样访问组。

string = '''someline abc\n
            someother line\n
            name my_user_name is valid\n
            some more lines\n'''

pattern = r'name (?P<user>.*) is valid'
matches = re.search(pattern, str(string), re.DOTALL)
print(matches['user'])

# my_user_name

我通过谷歌找到了这个答案,因为我想用多个组直接将re.search()结果解压缩到多个变量中。虽然这对某些人来说可能是显而易见的,但对我来说却不是,因为我过去总是使用group(),所以它可能会帮助那些未来也不知道group*s*()的人。

s = "2020:12:30"
year, month, day = re.search(r"(\d+):(\d+):(\d+)", s).groups()