简体   繁体   English

使用正则表达式从字符串 object 中提取数据到 Python

[英]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.在我的例子中,数值可以从 01 到 999 不等,因此我必须使用正则表达式模块。 I am working with Python.我正在使用 Python。

Sorry I found a simple solution:抱歉,我找到了一个简单的解决方案:

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

I will return:我会回来:

['91', '99', '87', '87'] ['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/这个网站可以帮助很多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+)

  • \d matches a digit (equivalent to [0-9]) \d匹配一个数字(相当于[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*)

  • \s matches any whitespace character (equivalent to [\r\n\t\f\v ]) \s匹配任何空白字符(相当于 [\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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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