Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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正则表达式,当且仅当字符串以相同的小写字母开头和结尾时,它才会匹配字符串。以下内容似乎应该有效,但与每个字符串都匹配: (?p[a-z]).*p=st$ 如有任何建议,将不胜感激 ^([a-z]).*\1$ 这将需要反向参考。请参阅演示 我知道你要求一个正则表达式,但话说回来,很多人这样要求是因为他们认为这是做这些事情的唯一方法 >>> for s in ('test', 'foo', 'Test', ''): if s and s[0].

我试图创建一个Python正则表达式,当且仅当字符串以相同的小写字母开头和结尾时,它才会匹配字符串。以下内容似乎应该有效,但与每个字符串都匹配:

(?p[a-z]).*p=st$

如有任何建议,将不胜感激

^([a-z]).*\1$
这将需要
反向参考
。请参阅演示


我知道你要求一个正则表达式,但话说回来,很多人这样要求是因为他们认为这是做这些事情的唯一方法

>>> for s in ('test', 'foo', 'Test', ''):
    if s and s[0].islower() and s[0] == s[-1]:
        print('good:', s)

good: test
>>> for s in ('test', 'foo', 'Test', ''):
    if s and s[0].islower() and s[0] == s[-1]:
        print('good:', s)

good: test