Kevin
Kevin

Reputation: 137

replacing unknown value in string

I'm running a process that dumps a bunch of information to files in a directory. I run the same process later and do a diff on the directories to see what has changed. I'm getting a bunch of false changes due to a memory address.

For Example:

  1. Run 1 gives

    0xb7390dd0
    
  2. Run 2 gives

    0xb73909c8
    

I would like to be able to ignore the fact that the memory addresses are different? What is the best way to accomplish this?

I can't use .replace() as I don't know what the address will be beforehand.

Upvotes: 1

Views: 223

Answers (1)

avasal
avasal

Reputation: 14854

you can create a regex to match the pattern of value and replace the matched value

>>> pattern = r'0x\w{8}'
>>> matcher = re.compile(pattern)
>>> matcher.match('0xb73909c8: has the error')
<_sre.SRE_Match object at 0x01E25288>
>>> matcher.match('0xb73909c8: has the error').group()
'0xb73909c8'

then you can do

>>> '0xb73909c8: has the error'.replace(matcher.match('0xb73909c8: has the error').group(), 'Address')
'Address: has the error'

Upvotes: 1

Related Questions