Home > front end >  Python regex where the desired match could be either one or two integers
Python regex where the desired match could be either one or two integers

Time:07-13

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))

CodePudding user response:

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

CodePudding user response:

You don't need that much regex, one can handle all cases, then use a capturing group (parenthesis) to fetch only the numeric value

def volts_from_filename(filename):
    file_volts, max_volts = 0, 0

    result = re.search(r'[_-](\d{1,2})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
File Volts: 16   Max volts : 15 volts

File Volts: 8   Max volts : 7 volts

CodePudding user response:

Try the website regex101.com, this helps me a lot when I am learning regexp. the best thing is that it has detail explanation and samples on why the regexp is doing what it does.

As for you issue, I believe you want:

  1. start with - or _
  2. followed by 1 or 2 digits
  3. an optional G
  4. end by an L

[-_][0-9]{1,2}G?L

This above should be what you need. You can use the website above to learn what it means, and you can also paste sample text to test if it actually works for you.

  • Related