python regex:从字符串中获取结束数字

时间:2012-11-22 19:18:08

标签: python regex

我对python和正则表达式(regex newbie here)很新,我有以下简单的字符串:

s=r"""99-my-name-is-John-Smith-6376827-%^-1-2-767980716"""

我想只提取上面字符串中的最后一位数字,即767980716,我想知道如何使用python正则表达式实现这一点。

我想做类似的事情:

re.compile(r"""-(.*?)""").search(str(s)).group(1)

表示我想找到介于两者之间的东西(。*?),以“ - ”开头并以字符串结尾结束 - 但这不返回任何内容..

我想知道是否有人能指出我正确的方向.. 感谢。

7 个答案:

答案 0 :(得分:32)

您可以使用re.match仅查找字符:

>>> import re
>>> s=r"""99-my-name-is-John-Smith-6376827-%^-1-2-767980716"""
>>> re.match('.*?([0-9]+)$', s).group(1)
'767980716'

或者,re.finditer同样适用:

>>> next(re.finditer(r'\d+$', s)).group(0)
'767980716'

所有正则表达式组件的说明:

  • .*?a non-greedy match并且只消耗尽可能多(贪婪的匹配会消耗除最后一位数之外的所有内容)。
  • [0-9]\d是捕获数字的两种不同方式。请注意,后者也matches digits in other writing schemes,如୪或2。
  • 括号(())使表达式的内容成为一个组,可以使用group(1)检索(或者第二组为2,整个匹配为0)。
  • +表示多个条目(最后至少有一个数字)。
  • $仅匹配输入的结尾。

答案 1 :(得分:7)

使用findall很简单:

import re

s=r"""99-my-name-is-John-Smith-6376827-%^-1-2-767980716"""

print re.findall('^.*-([0-9]+)$',s)

>>> ['767980716']

正则表达式解释:

^         # Match the start of the string
.*        # Followed by anthing
-         # Upto the last hyphen
([0-9]+)  # Capture the digits after the hyphen
$         # Upto the end of the string

或者更简单地只是匹配字符串末尾的数字 '([0-9]+)$'

答案 2 :(得分:6)

您的Regex应为(\d+)$

  • \d+用于匹配数字(一个或多个)
  • $用于匹配字符串的结尾。

所以,你的代码应该是: -

>>> s = "99-my-name-is-John-Smith-6376827-%^-1-2-767980716"
>>> import re
>>> re.compile(r'(\d+)$').search(s).group(1)
'767980716'

此处您不需要使用str功能,因为s已经是字符串。

答案 3 :(得分:4)

使用以下正则表达式

\d+$

$描绘了字符串的结尾..

\d是一个数字

+与前面的字符1匹配多次

答案 4 :(得分:3)

将正则表达式保存为需要更多繁重的东西。

>>> def parse_last_digits(line): return line.split('-')[-1]
>>> s = parse_last_digits(r"99-my-name-is-John-Smith-6376827-%^-1-2-767980716")
>>> s
'767980716'

答案 5 :(得分:2)

请尝试使用\d+$。匹配一个或多个数字字符,后跟字符串的结尾。

答案 6 :(得分:0)

我一直在尝试这些解决方案中的几种,但是如果字符串末尾没有数字,那么许多解决方案似乎都会失败。以下代码应该可以使用。

import re

W = input("Enter a string:")
if re.match('.*?([0-9]+)$', W)== None:
    last_digits = "None"
else:
    last_digits = re.match('.*?([0-9]+)$', W).group(1)
print("Last digits of "+W+" are "+last_digits)