Home > Blockchain >  Regex - System Requirements
Regex - System Requirements

Time:07-20

I am learning regex and having difficulty when finding the pattern from the system requirements string below:

"OS: Windows® 7 Processor: Intel Core i3-9100 / AMD Ryzen 3 2300X Memory: 8 GB RAM Graphics: NVIDIA® GeForce® GTX 1050 Ti / AMD Radeon™ RX 560 (4GB VRAM) Storage: 60 GB available space"

I have done multiple ways but couldn't find any match. I wanted to group the result based on the colon(:) to a python dictionary like this:

    {
        'OS': 'Windows® 7',
        'Processor': 'Intel Core i3-9100 / AMD Ryzen 3 2300X',
        'Memory': '8 GB RAM Graphics: NVIDIA® GeForce® GTX 1050 Ti / AMD Radeon™ RX 560 (4GB VRAM)',
        'Storage': '60 GB available space'
    }

Any help would be appreciated. Here is my work: regex101. Thank you.

CodePudding user response:

You can use re.findall with "(\w ):\s (.*?)(?=$|\s*\w :\s )": A word, followed by a colon and spaces, then as little as possible of anything until either the end of string or another word followed by a colon and spaces.

minimal example:

s = "OS: Windows® 7 Processor: Intel Core i3-9100 / AMD Ryzen 3 2300X Memory: 8 GB RAM Graphics: NVIDIA® GeForce® GTX 1050 Ti / AMD Radeon™ RX 560 (4GB VRAM) Storage: 60 GB available space"

import re
d = dict(re.findall(r"(\w ):\s (.*?)(?=$|\s*\w :\s )", s))

output:

{'OS': 'Windows® 7',
 'Processor': 'Intel Core i3-9100 / AMD Ryzen 3 2300X',
 'Memory': '8 GB RAM',
 'Graphics': 'NVIDIA® GeForce® GTX 1050 Ti / AMD Radeon™ RX 560 (4GB VRAM)',
 'Storage': '60 GB available space'}
  • Related