Python Forum
Using regex for type validation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using regex for type validation
#5
(May-22-2021, 07:35 AM)Gribouillis Wrote: You would get a better structured code with a function to handle each line
def is_vcf_line(line):
    columns = line.split('\t')
    return bool(
        re.match(r"^chr(?:[1-9][0-9]?|[XYM])$", columns[0]) 
        and re.match(r"^[1-9][0-9]*$", columns[1])
        and re.match(r"^[ATGC]$", columns[3]) 
        and re.match(r"^[ATGC]$", columns[4]))

def is_vcf(filename):
    with open(filename) as lines:
        return all(is_vcf_line(line) for line in lines)

..gotcha, but still when I run the tests I get false even thou I should get true
BTW, maybe I didnt mentioned it but but the second column need to be int as well, does the code check that as well?
Reply


Messages In This Thread
Using regex for type validation - by ranbarr - May-21-2021, 04:53 PM
RE: Using regex for type validation - by ranbarr - May-21-2021, 08:16 PM
RE: Using regex for type validation - by ranbarr - May-22-2021, 09:39 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020