Search code examples
pythonstringmatching

Why does multiple sequence of wildcard characters not work in re.search?


I am trying to use re.search but with multiple wildcard characters and it does not work. Is there something else I have to do? Or is there a better method?

import re
pattern = '2A-CS-*.GPM.DPR.V*'
file = '/home/files/2A-CS-WFF.GPM.DPR.V9-20240130.20240707-S220512-E220609.058829.V07C.HDF5'
print(re.search(pattern, file)) #returns None

Solution

  • This is a regular expression, not a shell glob (or similar wildcard-supporting syntax). * is not a wildcard, it's a postfix operator that says "zero or more occurrences of the pattern immediately preceding this".

    In regular expressions, .* is the equivalent of a wildcard.