Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.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 - Fatal编程技术网

仅匹配正则表达式python的一部分

仅匹配正则表达式python的一部分,python,regex,Python,Regex,我正在使用python的正则表达式,并且我正试图找到某个单词前面的匹配项。例如,在字符串“match.function”中,我只对“match”感兴趣 我应该如何编写正则表达式 谢谢我不确定你是指“.function”字面意思还是作为一个例子,但这里有两种方式: # literally function re.findall('w*(?=\.function)', s) # function as example re.finall('\w*(?=\.\w*)', s) 这被称为正向前瞻 试

我正在使用python的正则表达式,并且我正试图找到某个单词前面的匹配项。例如,在字符串“match.function”中,我只对“match”感兴趣

我应该如何编写正则表达式

谢谢

我不确定你是指“.function”字面意思还是作为一个例子,但这里有两种方式:

# literally function
re.findall('w*(?=\.function)', s)

# function as example
re.finall('\w*(?=\.\w*)', s)

这被称为正向前瞻

试试这个:

(\w+)\.(?=function)
在python中:

import re
pattern=r'(\w+)\.(?=function)'
string1='match.function'
print(re.search(pattern,string1).group(1))
输出:

match

了解捕获组