Skip to content
Advertisement

Regex pattern for filename with date

I have files with name “data_2021_03_v1.0.zip” in server. When I tried using “data.*(ZIP|zip)” regex.It is loading all files starting with data string. I want files containing only data(exactmatch-no extra words along with it) in filename.

Ex: It is loading files with name “data_abc_efg_19-01-v2.0.zip” along with original file. Can someone help to construct a regex pattern for above file format

Advertisement

Answer

You can make the pattern more specific

bdata_d{4}_d{2}_vd+(?:.d+)*.(?:ZIP|zip)b
  • bdata_ A word boundary to prevent a partial match, then match data_
  • d{4}_d{2}_v Match 4 digits _ 2 digits _v
  • d+(?:.d+)* Match 1+ digits and optionally repeat a . and 1+ digits
  • .(?:ZIP|zip) Match either .zip or .ZIP
  • b A word boundary

Regex demo

You could make the pattern case insensitive, but then you would also match DATA for example

(?i)bdata_d{4}_d{2}_vd+(?:.d+)*.zipb
Advertisement