简体   繁体   中英

Extract data from string object with regex into Python

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.

Sorry I found a simple solution:

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

I will return:

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

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

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