仅匹配正则表达式python的一部分
我正在使用python的正则表达式,并且我正试图找到某个单词前面的匹配项。例如,在字符串“match.function”中,我只对“match”感兴趣 我应该如何编写正则表达式 谢谢我不确定你是指“.function”字面意思还是作为一个例子,但这里有两种方式:仅匹配正则表达式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) 这被称为正向前瞻 试
# 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
了解捕获组