Complex regex in Python -


i trying write generic pattern using regex fetches particular things string. let's have strings gigabitethernet0/0/0/0 or fastethernet0/4 or ethernet0/0.222. regex should fetch first 2 characters , numerals. therefore, fetched result should gi0000 or fa04 or et00222 depending on above cases.

x = 'gigabitethernet0/0/0/2 m = re.search('([\w+]{2}?)[\\\.(\d+)]{0,}',x) 

i not able understand how shall write regular expression. values can fetched in form of list also. write few more patterns isn't helping.

in regex, may use re.findall function.

>>> import re >>> s = 'gigabitethernet0/0/0/0 ' >>> s[:2]+''.join(re.findall(r'\d', s)) 'gi0000' 

or

>>> ''.join(re.findall(r'^..|\d', s)) 'gi0000' >>> ''.join(re.findall(r'^..|\d', 'ethernet0/0.222')) 'et00222' 

or

>>> s = 'gigabitethernet0/0/0/0 ' >>> s[:2]+''.join([i in s if i.isdigit()]) 'gi0000' 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -