Home > Enterprise >  using regex to find a ether address
using regex to find a ether address

Time:07-15

I'm trying to use a regex expression to find a ether address. im pretty new to regex so id apparated a bit of help explaining why my code is returning a null value. its probably something to do with the expression its self. row has two ether address in it.

row = 'afdsf1 asdfasdf0xc7d688cb053c19ad5ee4f48c348958880537835fdsgdsfg 0xc7d688cb053c19ad5ee4f48c348958880537835f'
all_match = re.findall(pattern= '^0x[a-fA-F0-9]{40}$', string=row)
for match in all_match:
    print(match.group())
print(all_match) 

CodePudding user response:

row = 'afdsf1 asdfasdf0xc7d688cb053c19ad5ee4f48c348958880537835fdsgdsfg 0xc7d688cb053c19ad5ee4f48c348958880537835f'
all_match = re.findall(r'0x[0-9a-f]{40}', row)
for match in all_match:
    print(all_match)

You can't call .group() here since str does not have that attribute

CodePudding user response:

You need to update the regex and remove ^. re.findall() will return list of strings with this pattern '0x[a-fA-F0-9]{40}$'.

row = 'afdsf1 asdfasdf0xc7d688cb053c19ad5ee4f48c348958880537835fdsgdsfg 0xc7d688cb053c19ad5ee4f48c3z48958880537835f'
all_match = re.findall(pattern='0x[a-fA-F0-9]{40}$', string=row)
for match in all_match:
    print(match)

0xc7d688cb053c19ad5ee4f48c348958880537835f

  • Related