Python Forum

Full Version: re.sub(r"\W", v, s) vs. re.sub(r"\W+", v, s)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Is there any difference between re.sub(r"\W", v, s) and re.sub(r"\W+", v, s)? Won't they both equally replace all non-alphanumeric characters in s with v?
Yes, there's a difference because each replacement is done separately.

The first example will replace every non-word character (\W) with your replacement string.

>>> re.sub(r"\W", "X", "Hello, there!")
'HelloXXthereX'
The second will replace every group of consecutive non-word characters with a single instance of the replacement string.

>>> re.sub(r"\W+", "X", "Hello, there!")
'HelloXthereX'