Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex string doesn't match

Tags:

python

regex

I'm having trouble matching a digit in a string with Python. While it should be clearly matched, It doesn't even match [0-9] [\d] or just 0 alone. Where is my oversight?

import re

file_without_extension = "/test/folder/something/file_0"

if re.match("[\d]+$", file_without_extension):
   print "file matched!"
like image 760
F. Rakes Avatar asked Sep 08 '25 19:09

F. Rakes


2 Answers

Read the documentation: http://docs.python.org/2/library/re.html#re.match

If zero or more characters at the beginning of string

You want to use re.search (or re.findall)

like image 90
Explosion Pills Avatar answered Sep 10 '25 07:09

Explosion Pills


re.match is "anchored" to the beginning of the string. Use re.search.

like image 43
alexis Avatar answered Sep 10 '25 09:09

alexis