【问题标题】:python regex: get end digits from a stringpython regex:从字符串中获取结束数字
【发布时间】:2012-11-11 05:11:05
【问题描述】:

我对 python 和正则表达式很陌生(这里是正则表达式新手),我有以下简单的字符串:

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

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

我想做类似的事情:

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

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

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

【问题讨论】:

    标签: python regex


    【解决方案1】:

    您可以使用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,如 ୪ 或 ൨。
    • 括号 (()) 使表达式的内容成为一个组,可以使用group(1) 检索(或第二组为 2,整个匹配为 0)。
    • + 表示多个条目(末尾至少一个数字)。
    • $ 仅匹配输入的结尾。

    【讨论】:

      【解决方案2】:

      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]+)$'

      【讨论】:

        【解决方案3】:

        您的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 已经是一个字符串了。

        【讨论】:

        • 如果您将正则表达式模式写为r'(\d+)$',那么您不必转义反斜杠。
        【解决方案4】:

        使用下面的正则表达式

        \d+$
        

        $ 描述字符串的结尾..

        \d 是一个数字

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

        【讨论】:

          【解决方案5】:

          为需要更繁重的工作保存正则表达式。

          >>> 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'
          

          【讨论】:

            【解决方案6】:

            我一直在尝试其中的几个解决方案,但如果字符串末尾没有数字数字,许多解决方案似乎都失败了。以下代码应该可以工作。

            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)
            

            【讨论】:

            • m = re.findall(r"\d+\s*$", W); last_digits = m[0] if m else 'None' 消除了多余的表达式匹配。
            【解决方案7】:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2023-01-07
              • 2015-03-23
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多