简体   繁体   中英

Python Regex - match the last group

I have a string that looks like this:

a = '2017-11-02T00:00:10'

I want to match the last group, which is 10 in my string.

So, something like this: 2017-11-02T00:00: and 10

I tried re.match(r':\\d+$', a) , re.match(r':00$', a) , re.match(r':00+$', a)

But none of them seems to work. Can anyone please explain why my regex expression isn't working, and how to capture the last group from my string?

  • The output can be lenient as long as I can confirm that the last millisecond equals 10

This problem doesn't need regex and can be solved easily with .split()

a = '2017-11-02T00:00:10'
milliseconds = a.split(':')[-1]
print(milliseconds)
>>>"10"

Timing:

%timeit '2017-11-02T00:00:10'.split(':')[-1]
265 ns ± 3.86 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

If you really want to take the regex route which is a lot slower:

import re
a = '2017-11-02T00:00:10'

m = re.findall(r"\d+$", a)
print(m[0])
>>>10

This is because re.match() implicitly anchors at the beginning of the string , meaning it only succeeds if the match appears there. Because it anchors at the beginning of the string but not the end of the string, this behavior can be confusing. You may find it simpler to always use re.search() instead of re.match() , and to use ^ and/or $ when you need anchoring.

Regular expression is generally not the best solution (in term of perfs). If there are tools for do the job you should use them. Here's datetime.strptime is your friend:

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

Timing:

%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)

See also strftime() and strptime() Behavior .

You may try this too

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM