Check if a change has occurred in re.sub

The Python re.sub(pattern, replacement, string) function returns the changed string with the replaced pattern with replacement. Is there an easy way to check if a match has occurred and what modification has been made? (And also how many changes)

+4
source share
1 answer

Depends on the version. In <= 2.6 you need to connect sub() with match() or search() to get the score.

If you are using Python 2.7, you can use subn() , which will return a tuple (new_string, number_of_subs_made).

Not sure about 3+.

+4
source

Source: https://habr.com/ru/post/1483261/


All Articles