将整行代码(字符串格式)与 Python 正则表达式匹配
Matching entire line of code (string format) with Python regex
使用 Python 和正则表达式,我有一个字符串变量 strSourceCode,其内容如下
'\n function balanceOf(address account) external view returns (uint256);\r\n function transfer(address recipient, uint256 amount) 外部returns (bool) ;\r\n function allowance(address owner, address spender) external view returns (uint256);\r\n function approve(address spender, uint256 amount) 外部 returns (bool );\r\n function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);\r'
我想使用 re.findall 例如匹配单词 'allowance' 并且它应该 return 返回以 \n 开头并以 \r 结尾的整行代码,如下所示
'\n 函数 allowance(address owner, address spender) 外部视图 returns (uint256);\r'
我已经苦苦挣扎了几个小时,并决定 post。任何帮助都会很棒!谢谢。
我也试过 re.M 标志,但它似乎不起作用,我肯定做错了什么。
以下正则表达式将查找以“\n”开头、keyword/key 短语前后有任何内容并以“\r”结尾的所有匹配项。
import re
strSourceCode = '\n function balanceOf(address account) external view returns (uint256);\r\n function transfer(address recipient, uint256 amount) external returns (bool);\r\n function allowance(address owner, address spender) external view returns (uint256);\r\n function approve(address spender, uint256 amount) external returns (bool);\r\n function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);\r'
what_to_match = 'allowance'
pattern = f'\n .*{what_to_match}.*\r'
matches = re.findall(pattern, strSourceCode)
使用 Python 和正则表达式,我有一个字符串变量 strSourceCode,其内容如下
'\n function balanceOf(address account) external view returns (uint256);\r\n function transfer(address recipient, uint256 amount) 外部returns (bool) ;\r\n function allowance(address owner, address spender) external view returns (uint256);\r\n function approve(address spender, uint256 amount) 外部 returns (bool );\r\n function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);\r'
我想使用 re.findall 例如匹配单词 'allowance' 并且它应该 return 返回以 \n 开头并以 \r 结尾的整行代码,如下所示
'\n 函数 allowance(address owner, address spender) 外部视图 returns (uint256);\r'
我已经苦苦挣扎了几个小时,并决定 post。任何帮助都会很棒!谢谢。
我也试过 re.M 标志,但它似乎不起作用,我肯定做错了什么。
以下正则表达式将查找以“\n”开头、keyword/key 短语前后有任何内容并以“\r”结尾的所有匹配项。
import re
strSourceCode = '\n function balanceOf(address account) external view returns (uint256);\r\n function transfer(address recipient, uint256 amount) external returns (bool);\r\n function allowance(address owner, address spender) external view returns (uint256);\r\n function approve(address spender, uint256 amount) external returns (bool);\r\n function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);\r'
what_to_match = 'allowance'
pattern = f'\n .*{what_to_match}.*\r'
matches = re.findall(pattern, strSourceCode)