Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Python by (47.6k points)

I am trying to do a grab everything after the "</html>" tag and delete it, but my code doesn't seem to be doing anything. Does .replace() not support regex?

z.write(article.replace('</html>.+', '</html>'))

2 Answers

0 votes
by (106k points)

You can not do as you want because regular expressions in Python are handled by the re module see the code below:-

article = re.sub(r'(?is)</html>.+', '</html>', article)

0 votes
by (20.3k points)

You can try using the re module for regexes, but regexes are probably overkilled for what you want. You can do something like this: 

z.write(article[:article.index("</html>") + 7]

This will be much cleaner and should be much faster than a regex-based solution.

Related questions

+1 vote
1 answer
0 votes
1 answer
0 votes
1 answer
asked Oct 4, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...