Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python正则表达式_Python_Regex_Function - Fatal编程技术网

python正则表达式

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

我的问题是,如何在函数中应用“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!")

稍微检查一下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)