python正则表达式
我的问题是,如何在函数中应用“info”, 我不知道怎么做python正则表达式,python,regex,function,Python,Regex,Function,我的问题是,如何在函数中应用“info”, 我不知道怎么做 如果您有任何帮助,我们将不胜感激我想这就是您想要的: file = open("demo.txt","r") text = file.read() def find(info): match = re.findall(r"info+\w+\d+",text) if match: print(match) else: print("Not found!") 稍微检查一下Pytho
如果您有任何帮助,我们将不胜感激我想这就是您想要的:
file = open("demo.txt","r")
text = file.read()
def find(info):
match = re.findall(r"info+\w+\d+",text)
if match:
print(match)
else:
print("Not found!")
稍微检查一下Python语法
file = open("demo.txt","r")
text = file.read()
def find(info,text):
match = re.findall(info + "+\w+\d+",text)
if match:
print(match)
else:
print("Not found!")
# This is how you call the function
find("whatever info is supposed to be",text)
你能说清楚点吗?该函数接受参数
info
,但您正在为findall
函数提供参数text
…很抱歉造成混淆!这仍然使用了re.findall
中的全局变量text
,而不是函数参数info
@isosceleswheel:Well spotted-代码相应地进行了调整非常感谢您的帮助!!
# beware "file" is a python keyword so you might want to use a different name
file = open("demo.txt","r")
# your text is called "text" so that is the argument to use in the function call
text = file.read()
# define the function:
def find(info):
# your variable is called "info" in the function so call findall on "info"
match = re.findall(r"info+\w+\d+",info)
if match:
print(match)
else:
print("Not found!")
# call the function on "text" since that is the name of the local variable you created with file.read()::
find(text)