Match integer values from end of string until second dot

Question:

I have following string GA1.2.4451363243.9414195136 and I want to match 4451363243.9414195136 using regular expression for python.

I have tried the following which is not working ([d].[d])$

Where am I going wrong here?

Answers:

A few ideas (string operations or regex):

s = 'GA1.2.4451363243.9414195136'

out = '.'.join(s.rsplit('.', 2)[-2:])
# '4451363243.9414195136'

import re
out = re.search(r'[^.]*.[^.]*$', s)
# <re.Match object; span=(6, 27), match='4451363243.9414195136'>

NB. to ensure matching digits, you can replace [^.] (any character but .) with d.

For an arbitrary N:

N = 3

out = '.'.join(s.rsplit('.', N)[-N:])
# '2.4451363243.9414195136'

out = re.search(fr'[^.]*(?:.[^.]*){{{N-1}}}$', s)
# <re.Match object; span=(4, 27), match='2.4451363243.9414195136'>
Answered By: mozway

The following regex ([0-9]+.[0-9]+)$ matches the expected part of the example. Note that more specific solutions may arise as you provide more details, restrictions, etc. regarding the part to be matched:

>>> import re
>>> data = "GA1.2.4451363243.941419513"
>>> re.findall(r"([0-9]+.[0-9]+)$", data)
['4451363243.941419513']

It requests the matched part to be made of:

  • digit(s)
  • dot
  • digit(s)
  • end of line.
Answered By: Jib

It could be done using pure python! but if you want to use regex here is the code:

regex:

(?:[wd]*.){2}(.*)

python:

import re
s = 'GA1.2.4451363243.9414195136'
re.match(r'(?:[wd]*.){2}(.*)',s).groups()[0]   # output: '4451363243.9414195136'

OR

Just use python:

s.split('.',2)[-1]    # output: '4451363243.9414195136'
Answered By: BSimjoo
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.