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:
'''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:
>>> 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:
: Match a colon[^:\n]*
: Match 0 or more of any character that is not :
and newline\n
: Match a new line)
: End 1st capture group\s*
: Match 0 or more whitespaces:
: Match a colon(.+)
: Match 1 or more of any characters (except newlines) in 2nd capture group\1\2
: Is used in replacement to put back substring captured in groups 1 and 2.Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments