Extracting numbers from a text file except numbers in specific context

Viewed 77

I am trying to extract all the numbers in a text file except the numbers from the date and the Total change. I currently have this regex pattern = re.compile(r'[-]?\d+[,]?\d+?') which extracts all the numbers. Can anybody suggest how to put exceptions in it.

My text file

Changes from:       August 18, 2020                                  Total Change is:    -3,029

         0     -2,872        -18        898        870       -200     -2,819       -311       -112          0          2          0       -778       -388
1 Answers

You may match the unwelcome strings and match and capture the numbers in all other contexts using the best regex trick ever:

results = [x for x in re.findall(r'Total Change is:\s*\S+|Changes from:.*?\d{4}\b|([-+]?\d+(?:,\d+)?)', text) if x]

See the regex demo. Regex details:

  • Total Change is:\s*\S+ - Total Change is:, zero or more whitespaces, one or more non-whitespace chars
  • | - or
  • Changes from:.*?\d{4}\b - Changes from:, zero or more chars other than line break chars as few as possible, four digits and a word boundary after
  • | - or
  • ([-+]?\d+(?:,\d+)?) - Group 1 (re.findall returns these values only): an optional + or -, one or more digits, and then an optional sequence of a comma and one or more digits

After you get the results with re.findall, you will just need to remove empty items from the result list.

Python demo:

import re
rx = r"Total Change is:\s*\S+|Changes from:.*?\d{4}\b|([-+]?\d+(?:,\d+)?)"
text = "Changes from:       August 18, 2020                                  Total Change is:    -3,029\n\n        0     -2,872        -18        898        870       -200     -2,819       -311       -112          0          2          0       -778       -388"
results = [x for x in re.findall(r'Total Change is:\s*\S+|Changes from:.*?\d{4}\b|([-+]?\d+(?:,\d+)?)', text) if x]
print(results)
# => ['0', '-2,872', '-18', '898', '870', '-200', '-2,819', '-311', '-112', '0', '2', '0', '-778', '-388']
Related