Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中使用正则表达式在字符串列表中查找匹配关键字后的下一个单词_Python_Regex_Lambda - Fatal编程技术网

在python中使用正则表达式在字符串列表中查找匹配关键字后的下一个单词

在python中使用正则表达式在字符串列表中查找匹配关键字后的下一个单词,python,regex,lambda,Python,Regex,Lambda,我有一个字符串列表,我想提取每个字符串中特定关键字后的下一个单词 当我使用lambda函数迭代列表时,我得到的是整个字符串,而不是关键字后面的下一个单词: import re s = ["The job ABS is scheduled by Bob.", "The job BFG is scheduled by Alice."] user = filter(lambda i:re.search('(?<=The job )(\w+)',i),s)

我有一个字符串列表,我想提取每个字符串中特定关键字后的下一个单词

当我使用lambda函数迭代列表时,我得到的是整个字符串,而不是关键字后面的下一个单词:

import re
s = ["The job ABS is scheduled by Bob.", "The job BFG is scheduled by Alice."]
user = filter(lambda i:re.search('(?<=The job )(\w+)',i),s)
print(*user)

output: The job ABS is scheduled by Bob. The job BFG is scheduled by Alice.
重新导入
s=[“作业ABS由Bob安排。”,“作业BFG由Alice安排。”]
user=filter(lambda i:re.search(')(?您可以简化此过程:

重新导入
arr=[“作业ABS由Bob安排”,“作业BFG由Alice安排。”]
user=[re.findall(')(?您可以使用

重新导入
s=[“作业ABS由Bob安排。”,“作业BFG由Alice安排。”]

rx=re.compile(r’(?
findall
如果只需要一个匹配项,就不需要了。如果它能将代码简化那么多就足够了
import re
s = "The job ABS is scheduled by Bob."
user = re.search('(?<=The job )(\w+)',s)
print(user.group())

output: ABS
[['ABS'], ['BFG']]
(['ABS'], ['BFG'])
rx = re.compile(r'The\s+job\s+(\w+)')
user = tuple(map(lambda x: x.group(1) or "", map(rx.search, s)))
('ABS', 'BFG')