繁体   English   中英

使用 REGEX Python 删除特定字符后的行

[英]Remove line after specific character with REGEX Python

我正在尝试使用 Python 中的 REGEX 和列​​表字符之后的行删除特定字符。

例如:

First Name:
Eric

我想删除“名字:”和“埃里克”,但留下之后的内容。

能否请你帮忙? 这只是删除第一行:

remove_list = ["First Name", "Last Name", "Age"]
for i in remove_list:
    rmv_regex = re.compile("(?m)^"+ i + ".*\n" +".*\n", re.IGNORECASE) # Ignore case, regex of lines that start with the keywords

要解决这个问题,只需启用 re.MULTILINE。 例如在本文中:

   First Name:
   Eric
   Last Name:
   Dirkson
   Age:
   34

用这个解决:

    remove_list = ["First Name", "Last Name", "Age"]
      for i in remove_list:
      rmv_regex = re.compile("(?m)^"+ i + ".*\n"+ ".*\n", re.IGNORECASE | re.MULTILINE)
      text = rmv_regex.sub('', text)

暂无
暂无

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

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