我已经看过其他问题了,但是我无法让这些答案适合我的情况。
我在文件中搜索了一个字符串。我想匹配确切的字符串,如果有匹配,做一些事情。我正在尝试为字符串制作正则表达式,“作者:”。如果找到这个字符串,将其从行中剥离并向我提供右边的所有内容,删除任何白色空格。有关如何实现此目的的以下代码的任何想法?。
metadata_author = re.compile(r'\bauthor:\b')
with open(tempfile, encoding='latin-1') as search:
for line in search:
result = metadata_author.search(line)
if result in line:
author = result.strip()
print(author)
答案 0 :(得分:1)
我会使用lookbehind(评论中提到的可能点的背面为负面):
metadata_author = re.compile(r'(?<=(?<!\.)\bauthor:).+')
with open(tempfile, encoding='latin-1') as search:
for line in search:
result = metadata_author.search(line)
if result:
author = result.group().strip()
print(author)
re.search
返回匹配对象,而不是字符串,因此要获取匹配的字符串,您必须调用result.group()
。
如果要删除所有空格(而不仅仅是修剪),请使用re.sub(r'\s*', '', result.group())
代替result.group().strip()
。