find ethereum addresses in a string using regex in python

To find all Ethereum addresses in a string using regex in Python, you can use the following code:

main.py
import re

# The regex pattern to match Ethereum addresses
pattern = r"0x[a-fA-F0-9]{40}"

# The input string to search
input_str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium 0x9bAAdbF5f9093EEB7Bf9620E74D7bf94c276CcD1 0x2aB04169FEe5eA24814B1B2b2418E232D70a9516"

# Find all matches to the pattern and print them
matches = re.findall(pattern, input_str)
print(matches)
480 chars
12 lines

The code uses the regular expression 0x[a-fA-F0-9]{40} to match Ethereum addresses. This pattern matches any string starting with "0x" followed by 40 hexadecimal characters. The re module is used to apply the pattern to the input string input_str, and the findall() method is used to find all non-overlapping matches. The output of the code will be a list of all Ethereum addresses found in the input string.

Note that this regex pattern will not match all valid Ethereum addresses, as there are a few edge cases where an address may not follow this exact format. However, it should work for the vast majority of addresses.

gistlibby LogSnag