sim_rum
sim_rum

Reputation: 51

Use regular expression to extract numbers from a string in Python

I am trying to write a regular expression in Python that extracts two numbers from following strings:

This is for generating overviews of how many tools are in use. Therefore I only need the numbers from the string. Within the string only the word "tool" changes depending on the amount of tools. I've tried several solutions to compile the string but struggle to finalize my solution.

[...]
       regexToolUsage = re.compile(r"\((\S)(\d+)(\S)(\d+)(\d)\)"))

[...]
       if ToolUsage != None:
            ToolsInTotal = ToolUsage.group(2).strip()
            ToolsInUse = ToolUsage.group(4).strip()

I expect the output 64 and 8 but receive following error: UnboundLocalError: local variable 'ToolsInTotal' referenced before assignment.

What I also tried is following expression:

re.compile(r"(Total of)(.+)( issued; Total of)(.+)(in use)\)")

This extracts "64 tools" & "8 tools" but I need just the number. I cannot add the word "tools" as it would not be recognized if it is only "1 tool". Can anybody provide some help?

Upvotes: 1

Views: 1044

Answers (2)

Booboo
Booboo

Reputation: 44053

import re

text = """(Total of 64 tools issued; Total of 8 tools in use)
(Total of 49 tools issued; Total of 13 tools in use)
(Total of 3 tools issued; Total of 1 tool in use)"""

l = [
    (m.group(1), m.group(2))
    for m in re.finditer(r'\(Total of (\d+) tools? issued; Total of (\d+) tools? in use\)', text)
    ]
print(l)

Prints:

[('64', '8'), ('49', '13'), ('3', '1')]

Upvotes: 0

andole
andole

Reputation: 286

This is how you can get all integer numbers from a string:

import re


s = "Total of 64 tools issued; Total of 8 tools in use"

r = re.findall("\d+", s)  # 'r' is now ['64', '8']

Upvotes: 1

Related Questions