[英]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 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
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.