Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python .replace() regex [duplicate]

Tags:

python

regex

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>')) 
like image 499
user1442957 Avatar asked Jul 13 '12 18:07

user1442957


People also ask

Can you use regex in replace Python?

To replace a string in Python, the regex sub() method is used. It is a built-in Python method in re module that returns replaced string. Don't forget to import the re module. This method searches the pattern in the string and then replace it with a new given expression.

How do you substitute in regex?

To perform a substitution, you use the Replace method of the Regex class, instead of the Match method that we've seen in earlier articles. This method is similar to Match, except that it includes an extra string parameter to receive the replacement value.

How do you replace a pattern in Python?

Any string data can be replaced with another string in Python by using the replace() method. But if you want to replace any part of the string by matching a specific pattern then you have to use a regular expression.

How do I remove repeated words from a string in Python?

1) Split input sentence separated by space into words. 2) So to get all those strings together first we will join each string in given list of strings. 3) Now create a dictionary using Counter method having strings as keys and their frequencies as values. 4) Join each words are unique to form single string.


1 Answers

No. Regular expressions in Python are handled by the re module.

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

In general:

text_after = re.sub(regex_search_term, regex_replacement, text_before) 
like image 66
Ignacio Vazquez-Abrams Avatar answered Sep 18 '22 17:09

Ignacio Vazquez-Abrams