-1

I want to write a regular expression in python which finds </td= > in the string and replace it with </td>. The position of the characters inside the angular brackets may change. for example: it may be </t= d> or </= td> and so on. One of the character in the angular brackets is whitespace. The regular expression should find any of these occurrences and replace it with </td>. There is on similar question on How do I search and replace a pattern in text using regular expression in Python 3? but I am unable to figure out the answer. How to do it?

1 Answer 1

0

try with this regex

<[\/td= ]{5}>

https://regex101.com/r/T549ej/2

import re
string_to_process=r'</t= d> or </= td>'
substitute_string=r'</td>'
td_regex=re.compile(r'<[\/td= ]{5}>')
td_regex.sub(substitute_string, string_to_process)

For what you are asking in comments, use this regex:

<[^>]*?t[^>]*?d[^>]*?\/[^>]*?>|<[^>]*?t[^>]*?\/[^>]*?d[^>]*?>|<[^>]*?d[^>]*?t[^>]*?\/[^>]*?>|<[^>]*?d[^>]*?\/[^>]*?t[^>]*?>|<[^>]*?\/[^>]*?t[^>]*?d[^>]*?>|<[^>]*?\/[^>]*?d[^>]*?t[^>]*?>

see here: https://regex101.com/r/T549ej/4

import re
string_to_process=r'</t= d> or </= td>'
substitute_string=r'</td>'
td_regex=re.compile(r'<[^>]*?t[^>]*?d[^>]*?\/[^>]*?>|<[^>]*?t[^>]*?\/[^>]*?d[^>]*?>|<[^>]*?d[^>]*?t[^>]*?\/[^>]*?>|<[^>]*?d[^>]*?\/[^>]*?t[^>]*?>|<[^>]*?\/[^>]*?t[^>]*?d[^>]*?>|<[^>]*?\/[^>]*?d[^>]*?t[^>]*?>')
td_regex.sub(substitute_string, string_to_process)
Sign up to request clarification or add additional context in comments.

6 Comments

This works. Just want to know if we can check whether the angular brackets contains characters /td at any position and we can replace the whole string with </td>. how to write a regular expression for this?
See now; besides the replacement, you want to know if is it the case that the three chars /td appears exactly in that order?
I want to check if the characters /,t and d are present inside the angular brackets at any position in any order then replace the whole string by </td>.
are you sure about that? at any position in any order? you have three chars, so, all possible orders are 6 (i.e 3! = 3*2*1)... Could be <...t...d.../...> , <...t.../...d...> , <...d...t.../...> , <...d.../...t...> , <.../...t...d...> and <.../...d...t...> , where ... is anything, even, nothing... is that what you want?
Yes Sir. Ho do we write a regular expression for this in python?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.