Home > database >  Extract data from string object with regex into Python
Extract data from string object with regex into Python

Time:11-29

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.

CodePudding user response:

Sorry I found a simple solution :

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

I will return :

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

CodePudding user response:

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

  • Related