Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: how to substitute and know whether it matched

Tags:

python

regex

I know that re.sub(pattern, repl,text) can substitute when pattern matches, and then return the substitute.

My code is:

text = re.sub(pattern, repl, text1)

I have to define another variable to check whether it is modified:

text2 = re.sub(pattern, repl, text1)
matches = text2 != text1
text1 = text2

It has issues, for example with: text1='abc123def' , pattern = '(123|456)', repl = '123'. After replacement, it is the same string, so matches is false, but it actually matches.

like image 603
guilin 桂林 Avatar asked Oct 19 '10 14:10

guilin 桂林


2 Answers

Use re.subn

Perform the same operation as sub(), but return a tuple (new_string, number_of_subs_made).

and then check the number of replacements that were made. For example:

text2, numReplacements = re.subn(pattern, repl, text1)
if numReplacements:
    # did match
else:
    # did not match
like image 171
AndiDog Avatar answered Oct 17 '22 16:10

AndiDog


The repl parameter can also be a function which takes an RE match object and returns what the replacement should be; this function is not called if the text doesn't match. You could use that to do what you needed then just return a constant string you want to replace it with. This would cut down on an unneeded second check against the RE.

like image 29
user470379 Avatar answered Oct 17 '22 16:10

user470379