简体   繁体   English

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

[英]Remove line after specific character with REGEX Python

I am trying to remove specific characters with REGEX in Python and the line which comes after the character of a list.我正在尝试使用 Python 中的 REGEX 和列​​表字符之后的行删除特定字符。

For instance:例如:

First Name:
Eric

I would like to remove both "First Name:" AND "Eric" but leave what comes afterwards.我想删除“名字:”和“埃里克”,但留下之后的内容。

Could you please help?能否请你帮忙? This just deletes the first line:这只是删除第一行:

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

To solve this question, just enable re.MULTILINE.要解决这个问题,只需启用 re.MULTILINE。 For instance in this text:例如在本文中:

   First Name:
   Eric
   Last Name:
   Dirkson
   Age:
   34

Solve with this:用这个解决:

    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.

相关问题 Python - 删除特定字符后的特定字符? - Python - Remove a specific after a specific character? 如何使用 python 中的正则表达式从多行字符串中删除特定字符 - How can I remove a specific character from multi line string using regex in python Python:删除特定字符(,)之后的所有内容 - Python : Remove everything after a specific character (,) python 正则表达式在新行后删除 - python regex remove after new line python正则表达式:行继续符后的意外字符 - python regex: unexpected character after line continuation character 在python中实现正则表达式时,行继续后出现意外字符 - Unexpected Character after line continuation while implementing regex in python Python正则表达式:替换所有出现的特定字符串,而不是在特定字符之后 - Python regex: substitute all occurrence of a certain string NOT after a specific character 如何在Python中使用正则表达式在特定字符之前和之后添加空格? - How to add space before and after specific character using regex in Python? 如何在 Python 中的字符串列表中的特定字符之后删除 substring - How to remove substring after a specific character in a list of strings in Python Python:向后翻阅一个字符串,然后删除特定字符后的所有内容 - Python : go backwards through a string, then remove everything after a specific character
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM