我有点新手,如果我做错了什么请见谅。
我正在学习正则表达式,并且正在学习这个课程:https://regexone.com/lesson/capturing_groups。
在Python解释器中,我尝试使用括号只捕获搜索字符串中.pdf部分之前的内容,但我的结果尽管使用了括号,但仍然将其捕获。我做错了什么?
import re
string_one = 'file_record_transcript.pdf'
string_two = 'file_07241999.pdf'
string_three = 'testfile_fake.pdf.tmp'
pattern = '^(file.+)\.pdf$'
a = re.search(pattern, string_one)
b = re.search(pattern, string_two)
c = re.search(pattern, string_three)
print(a.group() if a is not None else 'Not found')
print(b.group() if b is not None else 'Not found')
print(c.group() if c is not None else 'Not found')
返回file_record_transcript.pdf
file_07241999.pdf
Not found
但应该返回
file_record_transcript
file_07241999
Not found
谢谢!