Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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_Match - Fatal编程技术网

Python正则表达式-匹配最后一个组

Python正则表达式-匹配最后一个组,python,regex,match,Python,Regex,Match,我有一个字符串如下所示: a = '2017-11-02T00:00:10' 我想匹配最后一组,即字符串中的10 比如说:2017-11-02T00:00:和10 我尝试了re.match(r':\d+$,a),re.match(r':00$,a),re.match(r':00+$,a) 但它们似乎都不起作用。有人能解释一下为什么我的正则表达式不起作用,以及如何从字符串中捕获最后一个组吗 只要我能确认最后一毫秒等于10 这是因为,这意味着只有在匹配出现时才会成功。因为它锚定在字符串的开头,而

我有一个字符串如下所示:

a = '2017-11-02T00:00:10'
我想匹配最后一组,即字符串中的
10

比如说:
2017-11-02T00:00:
10

我尝试了
re.match(r':\d+$,a)
re.match(r':00$,a)
re.match(r':00+$,a)

但它们似乎都不起作用。有人能解释一下为什么我的正则表达式不起作用,以及如何从字符串中捕获最后一个组吗

  • 只要我能确认最后一毫秒等于
    10

    • 这是因为,这意味着只有在匹配出现时才会成功。因为它锚定在字符串的开头,而不是字符串的结尾,所以这种行为可能会令人困惑。您可能会发现总是使用
      re.search()
      而不是
      re.match()
      ,并且在需要锚定时使用
      ^
      和/或
      $
      更简单。

      此问题不需要正则表达式,可以使用.split()轻松解决

      时间:

      %timeit '2017-11-02T00:00:10'.split(':')[-1]
      265 ns ± 3.86 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
      
      %timeit datetime.strptime('2017-11-02T00:00:10', '%Y-%m-%dT%H:%M:%S').second
      10.7 µs ± 1.84 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)
      
      如果您真的想走速度慢得多的regex路线:

      import re
      a = '2017-11-02T00:00:10'
      
      m = re.findall(r"\d+$", a)
      print(m[0])
      >>>10
      
      你也可以试试这个

      import re
      a= '2017-11-02T00:00:10'
      splrex=re.compile(r':(?=\d+$)')
      print(splrex.split(a))                # output:  ['2017-11-02T00:00', '10']
      
      regx=re.compile(r'^(.*)(\b\d+)$')
      m= regx.match(a)
      print(m.group(1),m.group(2))          # output:  2017-11-02T00:00: 10
      

      正则表达式通常不是最佳解决方案(就性能而言)。如果有做这项工作的工具,你应该使用它们。这是你的朋友:

      >>> from datetime import datetime
      >>> date = datetime.strptime('2017-11-02T00:00:10', '%Y-%m-%dT%H:%M:%S')
      >>> date
      datetime.datetime(2017, 11, 2, 0, 0, 10)
      >>> date.second
      10
      
      时间:

      %timeit '2017-11-02T00:00:10'.split(':')[-1]
      265 ns ± 3.86 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
      
      %timeit datetime.strptime('2017-11-02T00:00:10', '%Y-%m-%dT%H:%M:%S').second
      10.7 µs ± 1.84 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)
      

      另请参见。

      感谢您添加时报:)