python 2.7 - How to better this regex? -


i have list of strings this:

/soccer/poland/ekstraklasa-2008-2009/results/ /soccer/poland/orange-ekstraklasa-2007-2008/results/ /soccer/poland/orange-ekstraklasa-youth-2010-2011/results/ 

from each string want take middle part resulting in respectively:

ekstraklasa orange ekstraklasa orange ekstraklasa youth 

my code here job feels can done in fewer steps , regex alone.

name = re.search('/([-a-z\d]+)/results/', string).group(1) # take middle part name = re.search('[-a-z]+', name).group()                  # trim numbers if name.endswith('-'):     name = name[:-1]                                       # trim tailing `-` if needed name = name.replace('-', ' ') 

can see how make better?

this regex should work:

/(?:\/\w+){2}\/([\w\-]+)(?:-\d+){2}/ 

explanation:

  • (?:\/\w+){2} - eat first 2 words delimited /
  • \/ - eat next /
  • ([\w\-]+)- match word characters of hyphens (this we're looking for)
  • (?:-\d+){2} - eat hyphens , numbers after part we're looking for

the result in first match group


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -