正则表达式在python中查找最后一个组

时间:2015-03-25 12:52:59

标签: regex python-2.7

我想在下面的文字中找到最后一组。参见下面的例子

输入字符串

str1 =“计算燃油经济性,租赁费用或抵押贷款支付单击”查看“菜单,指向”工作表“,然后单击工作表以进行所需的计算.br_tag 2.在下面选择值想要计算,单击要计算的变量.br_tag 3.在文本框中输入已知值,然后单击Calculate..br_tag“

输出我想要

..br_tag

str2 =“计算燃油经济性,租赁或抵押付款单击”查看“菜单,指向”工作表“,然后单击要进行计算的工作表.br_tag 2.在”选择要计算的值“下,单击变量要计算的内容..br_tag 3.在文本框中输入已知值,然后单击Calculate..br_tag br_tag“

输出我想要

..br_tag br_tag

我尝试使用

re.compile(r'(\w\.(.*?))$')

但在这里我输出

  

t.br_tag 2.在选择要计算的值时,单击   要计算的变量.br_tag 3.输入已知值   在文本框中,然后单击Calculate..br_tag br_tag

1 个答案:

答案 0 :(得分:2)

使用正则表达式贪婪功能尽可能匹配所有字符。所以.*\w\.匹配从开头到最后一个点之前的所有chracater,前面有一个单词字符。现在,.*\w(\..*)$将捕获最后一个点前面有一个单词字符的所有字符。

>>> str1 = "Calculate fuel economy, lease, or mortgage payments Click the View menu, point to Worksheets, and then click the worksheet for the calculation you want.br_tag 2.Under Select the value you want to calculate, click the variable that you want to calculate..br_tag 3.Enter the known values in the text boxes and then click Calculate..br_tag"
>>> str2 = "Calculate fuel economy, lease, or mortgage payments Click the View menu, point to Worksheets, and then click the worksheet for the calculation you want.br_tag 2.Under Select the value you want to calculate, click the variable that you want to calculate..br_tag 3.Enter the known values in the text boxes and then click Calculate..br_tag br_tag"
>>> re.search(r'.*\w(\..*)$', str2).group(1)
'..br_tag br_tag'
>>> re.search(r'.*\w(\..*)$', str1).group(1)
'..br_tag'
>>> str3 = "Calculate fuel economy, lease, or mortgage payments Click the View menu, point to Worksheets, and then click the worksheet for the calculation you want.br_tag 2.Under Select the value you want to calculate, click the variable that you want to calculate..br_tag 3.Enter the known values in the text boxes and then click Calculate..br_tag .br_tag"
>>> re.search(r'.*\w(\..*)$', str3).group(1)
'..br_tag .br_tag'

您也可以使用lookbehinds

>>> re.search(r'(?<=\w)\.(?:(?<!\w)\.|[^.])*$', str1).group()
'..br_tag'
>>> re.search(r'(?<=\w)\.(?:(?<!\w)\.|[^.])*$', str2).group()
'..br_tag br_tag'
>>> re.search(r'(?<=\w)\.(?:(?<!\w)\.|[^.])*$', str3).group()
'..br_tag .br_tag'

DEMO