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

user11409134

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?

anubhava

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 Demo

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.

edited at
0

Comments

0 comments
Login to comment

Related

How can I remove a specific format from string with RegEx?

How to remove specific line from multi-line string

How can I remove a newline character or empty string from a list when using readline() with Python?

How can I remove a parenthesis that begins with a specific string using python?

How can I remove line breaks from lines that only start with a specific character in JavaScript?

Bash: How can i remove characters from a string until i hit a specific character

How to remove specific empty lines from multi line strings in python?

How can I remove everything beyond this 10 character alphanumeric string with notepad++ using regex

Extract list of words with specific character from string using regex in Python

How do I remove part of a string from a specific character?

How to get groupdict from multi-line string using regex

In python, how can I remove the last character in a print that is also using line continuation? Tried rstrip()

How to remove specific string from string using regex

In Python, how can I remove characters around a specific character?

How to remove a string that got a specific character in from a list in python

How can I use regex to remove a very specific portion of a string:

how to remove a specific character from a tuple using python

How can i replace multi line string using sed?

How can I remove all whitespace from a string except when part of said string is surrounded by a specific character in Node?

How can I remove the special character words from a string?

How can I remove every nth character from a string in javascript?

Using Perl with Regex, how can I remove a string within a string?

How to remove the first character in a line which contains a specific string from Linux command line?

How to remove a specific character from a string

How do I capture string between certain Character and String in multi line String? Python

Is there a way I can remove a character from a string?

How do I remove a character once from a string [python]?

How do I remove a specific line from a text file in python

Regex - remove a specific character from a string except the first after number