i am in need to use a variable in python regular expression
for line in re.findall('(.+)33084-2(.+)', Data):
i am using above code to match a line with 33084-2, some times this values changes so i need to use a variable like
for line in re.findall('(.+)Var_Name(.+)', Data):
please help me how to use a variable in python regular expression
Thanks in advance
If you know that the variable cannot contain any characters with special meaning in a regular expression just use string concatenation or any of the other ways to create a string.
for line in re.findall('(.+)'+Var_Name+'(.+)', Data):
If there is a chance the variable could contain special characters then escape it first:
for line in re.findall('(.+)'+re.escape(Var_Name)+'(.+)', Data):
试试这个简单的方法:
for line in re.findall('(.+){0}(.+)'.format(Var_Name), Data):
使用字符串格式化操作
for line in re.findall('(.+)%s(.+)' % Var_Name, Data):
The first argument to the findall function is just a string. So you use string concatenation:
for line in re.findall('(.+)'+Var_Name+'(.+)', Data):
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.