Add optional part in python regular expression -


i want add optional part python expression:

myexp = re.compile("(.*)_(\d+)\.(\w+)") 

so if string abc_34.txt, result.group(2) 34 if string abc_2034.txt, results.group(2) still 34

i tried myexp = re.compile("(.*)_[20](\d+)\.(\w+)")

but results.groups(2) 034 case of abc_2034.txt

thanks f.j.

but want expand solution , add suffix.

so if put abc_203422.txt, results.group(2) still 34

i tried "(.*)_(?:20)?(\d+)(?:22)?.(\w+)") 3422 instead of 34

myexp = re.compile("(.*)_(?:20)?(\d+)\.(\w+)") 

the ?: @ beginning of group containing 20 makes non-capturing group, ? after group makes optional. (?:20)? means "optionally match 20".


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -