简体   繁体   English

如何使用 python 中的正则表达式从多行字符串中删除特定字符

[英]How can I remove a specific character from multi line string using regex in python

I have a multiline string which looks like this:我有一个多行字符串,如下所示:

st = '''emp:firstinfo\n
       :secondinfo\n
       thirdinfo
     '''
print(st)

What I am trying to do is to skip the second ':' from my string, and get an output which looks like this:我想要做的是从我的字符串中跳过第二个':',并得到一个看起来像这样的 output:

'''emp:firstinfo\n
   secondinfo\n
   thirdinfo
   '''

simply put if it starts with a ':' I'm trying to ignore it.简单地说,如果它以':'开头,我试图忽略它。

Here's what I've done:这是我所做的:

mat_obj = re.match(r'(.*)\n*([^:](.*))\n*(.*)' , st)
print(mat_obj.group())

Clearly, I don't see my mistake but could anyone please help me telling where I am getting it wrong?显然,我没有看到我的错误,但谁能帮我告诉我哪里出错了?

You may use re.sub with this regex:您可以将re.sub与此正则表达式一起使用:

>>> print (re.sub(r'([^:\n]*:[^:\n]*\n)\s*:(.+)', r'\1\2', st))
emp:firstinfo
secondinfo

       thirdinfo

RegEx Demo正则表达式演示

RegEx Details:正则表达式详细信息:

  • ( : Start 1st capture group ( : 开始第一个捕获组
    • [^:\n]* : Match 0 or more of any character that is not : and newline [^:\n]* : 匹配 0 个或多个不是:和换行符的任何字符
    • : : Match a colon :匹配冒号
    • [^:\n]* : Match 0 or more of any character that is not : and newline [^:\n]* : 匹配 0 个或多个不是:和换行符的任何字符
    • \n : Match a new line \n : 匹配新行
  • ) : End 1st capture group ) : 结束第一个捕获组
  • \s* : Match 0 or more whitespaces \s* : 匹配 0 个或多个空格
  • : : Match a colon :匹配冒号
  • (.+) : Match 1 or more of any characters (except newlines) in 2nd capture group (.+) :匹配第 2 个捕获组中的 1 个或多个任意字符(换行符除外)
  • \1\2 : Is used in replacement to put back substring captured in groups 1 and 2. \1\2 :用于替换第 1 组和第 2 组中捕获的 substring。

You can use sub instead, just don't capture the undesired part.您可以使用 sub 代替,只是不要捕获不需要的部分。

(.*\n)[^:]*:(.*\n)(.*)

在此处输入图像描述

Replace by替换为

\1\2\3

Regex Demo


import re

regex = r"(.*\n)[^:]*:(.*\n)(.*)"

test_str = ("emp:firstinfo\\n\n"
    "       :secondinfo\\n\n"
    "       thirdinfo")

subst = "\\1\\2\\3"

# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
#import regex library

import re重新进口

#remove character in a String and replace with empty string. 

text = "The film Pulp Fiction was released in year 1994" result = re.sub(r"[az]", "", text) print(result) text = "电影低俗小说于 1994 年上映" result = re.sub(r"[az]", "", text) print(result)

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

相关问题 使用 REGEX Python 删除特定字符后的行 - Remove line after specific character with REGEX Python 如何从python中的字符中删除字符串? - How can I remove the string from a character in python? 如何从python正则表达式中排除特定字符串 - How can I exclude a specific string from from python regex 如何替换 python 中字符串中的特定字符? - How can I replace specific character from a string in python? 在 Python 中使用 readline() 时,如何从列表中删除换行符或空字符串? - How can I remove a newline character or empty string from a list when using readline() with Python? 使用正则表达式从python中的多行字符串中获取值 - Using regex to grab values from a multi-line string in python 我想使用 python 中的 strip() 和字符串理解方法从多行字符串中删除所有特殊字符 - I want to remove all special characters from a multi line string using strip() and string comprehension method in python 在Python中使用正则表达式从字符串中提取具有特定字符的单词列表 - Extract list of words with specific character from string using regex in Python 如何从python中的多行字符串中删除特定的空行? - How to remove specific empty lines from multi line strings in python? 如何在 Python 3 中的 assertRegex 中表达多行正则表达式? - How can I express a multi-line regex in assertRegex in Python 3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM