简体   繁体   English

Python 正则表达式 - 如何考虑行尾和文件尾并仅打印字符串的子部分?

[英]Python Regex - How to account for end-of-line and end-of-file AND print only subsection of string?

I have a list of names and a group assigned to them as dictated by the A, B, or C letters.我有一个由 A、B 或 C 字母指示的名称列表和分配给它们的组。 What I want to do is return all members of group A.我想要做的是返回 A 组的所有成员。

I am using a regex to find all lines that end with A, I then need to print the names of those individuals, not including the group (A, B, C)我正在使用正则表达式查找以 A 结尾的所有行,然后我需要打印这些人的姓名,不包括组(A、B、C)

I am running into a few issues:我遇到了几个问题:

  1. The very last entry is in group A, however this is not the end of a line but end of file and is being ignored.最后一个条目在 A 组中,但是这不是行尾而是文件尾并且被忽略。
  2. Some records contain a space before the end of line indicator and are being passed over.一些记录在行尾指示符之前包含一个空格,并且正在被传递。
  3. I only want to print the name and not the group.我只想打印名称而不是组。

Code代码

import re


   
test_str = ("John Doe: A\n"
    "Jane Washington: B\n"
    "Geoffrey Grupp: A \n"
    "Joseph Rose: A\n"
    "Victoria Georges: C \n"
    "Simon Murphy: A")

regex = r"^.*[A]$\n"    
result= re.findall(regex, test_str, re.MULTILINE)
result

Output Output

Out[8]: ['John Doe: A\n', 'Joseph Rose: A\n']

As you can see, I am missing Geoffrey Grupp and Simon Murphy.如您所见,我想念 Geoffrey Grupp 和 Simon Murphy。 Additionally, I do not want to print the ": A" after each name.此外,我不想在每个名称后打印“:A”。

You can try:你可以试试:

import re

test_str = ("John Doe: A\n"
    "Jane Washington: B\n"
    "Geoffrey Grupp: A \n"
    "Joseph Rose: A\n"
    "Victoria Georges: C \n"
    "Simon Murphy: A")

regex = r"^(.*): A *$"     
result= re.findall(regex, test_str, re.MULTILINE)
print(result)

It gives:它给:

['John Doe', 'Geoffrey Grupp', 'Joseph Rose', 'Simon Murphy']

Explanation:解释:

  • '(.*)' is a capture group - the part of the pattern which will be returned; '(.*)'是一个捕获组 - 将返回的模式部分;
  • ' *' matches possible space characters between A and the end of the line. ' *'匹配A和行尾之间可能存在的空格字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM