1

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

  • (Total of 64 tools issued; Total of 8 tools in use)
  • (Total of 49 tools issued; Total of 13 tools in use)

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?

sim_rum
  • 51
  • 7
  • 2
    That's because your `if` block does not get executed and you are using `ToolsInTotal` later on. – user2390182 Oct 02 '19 at 08:56
  • possible duplicate of https://stackoverflow.com/questions/26825729/extract-number-from-string-in-python/26825781 – ksha Oct 02 '19 at 08:59
  • Thanks @Ketan, but this did unfortunately not help me a lot. My problem is that I don't find the right solution for my regular expression.. – sim_rum Oct 02 '19 at 10:14

2 Answers2

1

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']
andole
  • 286
  • 1
  • 7
  • The string is one of many within one file. That is why I search for the specific string and afterwards want to extract the two numbers. – sim_rum Oct 02 '19 at 09:00
  • @sim_rum then why not be more specific: `re.search("Total of (?P\d+) tools issued; Total of (?P\d+) tools in use", s)`? – tomjn Oct 02 '19 at 09:08
  • @tomjn, I first want to define the correct regular expression to compile and afterwards search for it within the file.. – sim_rum Oct 02 '19 at 10:16
  • @sim_rum Ok, I am suggesting you do `regexToolUsage = re.compile("Total of (?P\d+) tools issued; Total of (?P\d+) tools in use")` if the sentence is always in that form – tomjn Oct 02 '19 at 10:18
  • This is not working for me, the only word that is changing is "Tools" instead of "Tool" if there are more than one (or 0). – sim_rum Oct 02 '19 at 11:12
  • ```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". – sim_rum Oct 02 '19 at 11:31
  • 1
    @sim_rum `re.compile("Total of (\d+) tool(?:s)? issued; Total of (\d+) tool(?:s)? in use")` – tomjn Oct 02 '19 at 11:39
0
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')]
Booboo
  • 38,656
  • 3
  • 37
  • 60