Home > front end >  how can i use re.findall to match a substring using python regex?
how can i use re.findall to match a substring using python regex?

Time:03-22

I have the following string variable called strSourceCode in python3.x,

'uint256 private constant _tTotal = 100000000 * 10**9;
uint256 private _rTotal = (MAX - (MAX % _tTotal));
_maxTxAmount = 300000000 * 10**9;'

I would like to match '100000000 * 10**' and'300000000 * 10**' and it should give me the following two lines as output using re.findall

uint256 private constant _tTotal = 100000000 * 10**9;
_maxTxAmount = 300000000 * 10**9;

Currently i have the following code:

pattern = '^.*[0-9]0{4,}.*$'
matches = re.findall(pattern, strSourceCode, re.MULTILINE)

which falsely outputs as:

_maxTxAmount = 300000000 * 10**9;0000 * 10**9;

Any help would be appreciated.

CodePudding user response:

We can try using re.findall here in multiline mode:

inp = """uint256 private constant _tTotal = 100000000 * 10**9;
uint256 private _rTotal = (MAX - (MAX % _tTotal));
_maxTxAmount = 300000000 * 10**9;"""

lines = re.findall(r'^.*\d \s*\*\s*\d \*\*\d .*$', inp, flags=re.M)
print(lines)

This prints:

['uint256 private constant _tTotal = 100000000 * 10**9;',
 '_maxTxAmount = 300000000 * 10**9;']

The regex pattern used here attempts to find any line containing an integer multiplied by 10 to some integer power.

CodePudding user response:

pattern = '[0-9]0{4,}[0-9\s*]*'
matches = re.findall(pattern, strSourceCode, re.MULTILINE)

It doesn't match entire lines. The pattern searches for digits followed by at least four zeroes which is then followed by any number of spaces, digits, and * characters.

If you want to match exactly * 10** after you big number you can do this:

pattern = '[0-9]0{4,} \* 10\*\*'
matches = re.findall(pattern, strSourceCode, re.MULTILINE)
  • Related