Search code examples
pythonregexstringregex-groupregex-greedy

How to use re.sub in Python?


Text = "<a> text </a> <c> code </c>"                                               

I want to remove <c> code </c> statement in python

output = "<a> text </a>"

Solution

  • You can use re.sub:

    >>> import re
    >>> text = "<a> text </a> <c> code </c>"
    >>> new_text = re.sub(r'<c>.*?</c>', '', text)
    >>> new_text
    <a> text </a>