About regular expressions

Explain the statement cleaned = re.sub('\W+', ' ', text)

The last text parameter is assigned to some text which you didn’t include here but that aside.

The basic translation of this expression is:
"Search the text string for a pattern of 1 or more occurrences of non-word characters (’\W+’) and replace those characters with a space (’ '). Then assign the result to a new variable called cleaned

In other words, search the 3rd parameter for occurrences of the 1st parameter and replace those occurrences with the 2nd parameter.

You can go here to type in any regular expression to see what it means: https://regex101.com/

\W+. matches any non-word character (equal to [^a-zA-Z0-9_])

2 Likes