Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I seem not to get it right in regex in python when matching digits

I have a list containing items that mirror file names. I would like to filter only the names that are contain only digits. I seem not to get it right. Even matching a a single digit seem not to be working. Am I the one not doing it right? Any leads or suggestions would be appreciated.

Sample of code:

import re

pattern = r"^\d.+[.]"
pattern2 = r'\d*'

a = ["1000.mp4", "test.mp4", "110082.mp4", "829873.m4a"]

for i in a:
    if re.match(i, pattern):
        print(i)
like image 262
Moses Avatar asked Jan 30 '26 16:01

Moses


1 Answers

This is a working code. You can fiddle around the regex to get a better regex string for matching without dot character.

import re

pattern = re.compile(r"^[0-9]+.")

a = ["1000.mp4", "test.mp4", "110082.mp4", "829873.m4a"]

for i in a:
    match = re.match(pattern, i)
    if match:
        matched_string = match.group(0)
        string_without_dot = matched_string[0:len(matched_string)-1]
        print(string_without_dot)
like image 138
moredrowsy Avatar answered Feb 01 '26 06:02

moredrowsy