-1

I have this string:

a = '91:99 OT (87:87)' 

I would like to split it into:

['91', '99', '87', '87']

In my case numerical values can vary from 01 to 999 so that I have to use regex module. I am working with Python.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
ahmedaao
  • 377
  • 1
  • 3
  • 14
  • Does this answer your question? [How to extract numbers from a string in Python?](https://stackoverflow.com/questions/4289331/how-to-extract-numbers-from-a-string-in-python) – mkrieger1 Nov 28 '22 at 22:12

2 Answers2

0

Sorry I found a simple solution :

re.findall('[0-9]+', a)

I will return :

['91', '99', '87', '87']

ahmedaao
  • 377
  • 1
  • 3
  • 14
0

regex is a pretty complicated topic. This site can help a lot https://regex101.com/ https://cheatography.com/davechild/cheat-sheets/regular-expressions/

I hope this is the answer you are looking for

(\d+)(?=\s*)

1st Capturing Group (\d+)

  • \d matches a digit (equivalent to [0-9])
  • + matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)

Positive Lookahead (?=\s*)

  • \s matches any whitespace character (equivalent to [\r\n\t\f\v ])
  • * matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
    import re

regex = r"(\d+)(?=\s*)"

test_str = "a = '91:99 OT (87:87)'"

matches = re.finditer(regex, test_str, re.MULTILINE)

for matchNum, match in enumerate(matches, start=1):
    
    print (" F{matchNum} : {match}".format(matchNum = matchNum, match = match.group()))
    
    for groupNum in range(0, len(match.groups())):
        groupNum = groupNum + 1

scotmanDavid
  • 139
  • 1
  • 9