[英]My regex formula misses one of the items I am trying to match. Is there something I am missing?
I have written this so far and it effectively captures 15 of the 16 students who got a 'B' grade.到目前为止,我已经写了这篇文章,它有效地捕捉了 16 名获得“B”级学生中的 15 名。 Unfortunately, the last student with a 'B' is left out.
不幸的是,最后一个获得“B”的学生被排除在外。 Is there something I am missing?
有什么我想念的吗?
import re
def grades():
with open ("assets/grades.txt", "r") as file:
grades = file.read()
#grades = re.findall(\w+\s\w+\:\s[B]\s+, grades)
grades = re.findall("(?P<title>\w+\s\w+)(?=\:\s[B]\s+)",grades)
print(grades)
Actually, you want to print all the line parts before :
if the part after :
is B
.实际上,您想打印
:
:
的部分是B
。
So, you can achieve this without a regex:所以,你可以在没有正则表达式的情况下实现这一点:
def grades():
with open ("assets/grades.txt", "r") as file:
grades = []
for line in file:
parts = line.strip().split(':')
if parts[1].strip() == 'B':
grades.append(parts[0].strip())
print(grades)
You may still get the list with the regex, using您仍然可以使用正则表达式获取列表,使用
import re
def grades():
with open ("assets/grades.txt", "r") as file:
grades = re.findall(r'^([^:\r\n]+):[^\S\r\n]*B$', file.read(), re.M)
print(grades)
where ^([^:\r\n]+):[^\S\r\n]*B$
matches其中
^([^:\r\n]+):[^\S\r\n]*B$
匹配
^
- start of a line ^
- 行首([^:\r\n]+)
- any zero or more chars other than :
and CR/LF ([^:\r\n]+)
- 除:
和 CR/LF 之外的任何零个或多个字符:
- a colon :
- 一个冒号[^\S\r\n]*
- zero or more horizontal whitespace [^\S\r\n]*
- 零个或多个水平空格B
- a B
B
- B
$
- end of a line. $
- 行尾。 See the regex demo .请参阅正则表达式演示。
See a Python demo :请参阅Python 演示:
file = """Ronald Mayr: A
Bell Kassulke: B
Alexander Zeller: C
Simon Loidl: B"""
def grades():
grades = []
for line in file.splitlines():
parts = line.strip().split(':')
if parts[1].strip() == 'B':
grades.append(parts[0].strip())
print(grades)
grades()
# => ['Bell Kassulke', 'Simon Loidl']
import re
print( re.findall(r'^([^:\r\n]+):[^\S\r\n]*B$', file, re.M) )
## => ['Bell Kassulke', 'Simon Loidl']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.