Home > Blockchain >  Regex to match the given formats
Regex to match the given formats

Time:10-04

i'm trying to match the below names

  • abc.cdf.ghi.klm-1.0.0.zip
  • abc.cdf.ghi.klm_config-1.0.0.18.zip

I have created ^abc.cdf.ghi.klm(-|_config)(\d*).(\d*).(\d*)(.(\d*).zip), but its only matching the first name. How to match the second name

CodePudding user response:

Your current pattern has multiple issues, including not being correct and also not escaping regex metacharacters. Consider this version:

^abc\.cdf\.ghi\.klm(?:_config)?-\d (?:\.\d )*\.zip$

Demo

Explanation:

^                   from the start of the filename
abc\.cdf\.ghi\.klm  match abc.cdf.ghi.klm
(?:_config)?        optionally followed by _config
-                   -
\d                  a number
(?:\.\d )*          optionally followed by dot/number zero or more times
\.zip               ending in .zip
$                   end of the filename
  • Related