Search code examples
pythonregexregex-group

Python regex where the desired match could be either one or two integers


I have some filenames with information I need toe extract, the information is either one or two numbers in the string. I have code to do either one number or 2 numbers but I cannot get it to work to handle both. I have looked at this Regex match one of two words but couldn't see how to adapt or use it.

Code

from os import listdir
from os.path import isfile, join
import re



mylist = [r"C:\...\A000-2021_6D_16GL_-do_not_use.txt",
          r"C:\...\A000-2021_6D_8GL_-do_not_use.txt"]

def volts_from_filename(filename):
   
    regexList = ['-\dL', '_\dL', '_\dGL', '-\dGL', '-\d\dL', '_\d\dL', '_\d\dGL', '-\d\dGL']

    for reggie in regexList:
        result = re.search(reggie, filename)
        print(f"result : {result}")
        if result:
            match = result.group(0)
            print(f"match {match}")
            reg_exp = '\d\d'
            result = re.search(reg_exp, match)
            file_volts = int(result.group(0))
            max_volts = file_volts - 1
    print(f"File Volts: {file_volts}   Max volts : {max_volts}volts")
    return file_volts, max_volts




for filename in mylist:
    print('\n'*2)
    volts_from_filename(filename)

returns

result : None
result : None
result : None
result : None
result : None
result : None
result : <re.Match object; span=(19, 24), match='_16GL'>
match _16GL
result : None
File Volts: 16   Max volts : 15volts



result : None
result : None
result : <re.Match object; span=(19, 23), match='_8GL'>
match _8GL

Desired returns

File Volts: 16   Max volts : 15volts

File Volts: 8   Max volts : 7volts

Error dialog

NoneType error object has no attribute 'group'.
Line 34 volts_from_filename(filename)
line 24 in volts from filename
    file_volts = int(result.group((0))

Solution

  • In the second case there is no match with reg_exp = '\d\d', as there is only one digit.

    You can however solve this with one regex that combines them all:

    def volts_from_filename(filename):
        result = re.search(r"[-_](\d+)G?L", filename)
        if result:
            file_volts = int(result.group(1))
            max_volts = file_volts - 1
            print(f"File Volts: {file_volts}   Max volts : {max_volts}volts")
            return file_volts, max_volts