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?
Add a comment
|
1 Answer
try with this regex
<[\/td= ]{5}>
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)
6 Comments
krishna bharadia
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?alsjflalasjf.dev
See now; besides the replacement, you want to know if is it the case that the three chars /td appears exactly in that order?
krishna bharadia
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>.alsjflalasjf.dev
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?
krishna bharadia
Yes Sir. Ho do we write a regular expression for this in python?
|