简体   繁体   中英

python regex find characters from and end of the string

svn-backup-test,2014/09/24/18/Rev1223/FullSvnCheckout.tgz

from the following string I need to fetch Rev1233. So i was wondering if we can have any regexpression to do that. I like to do following string.search ("Rev" uptill next /)

so far I split this using split array s1,s2,s3,s4,s5 = string ("/",4)

You don't need a regex to do this. It is as simple as:

 str = 'svn-backup-test,2014/09/24/18/Rev1223/FullSvnCheckout.tgz'
 str.split('/')[-2]

Here is a quick python example

>>> impot re
>>> s = 'svn-backup-test,2014/09/24/18/Rev1223/FullSvnCheckout.tgz'
>>> p = re.compile('.*/(Rev\d+)/.*')
>>> p.match(s).groups()[0]
'Rev1223'

Find second part from the end using regex, if preferred:

/(Rev\d+)/[^/]+$

http://regex101.com/r/cC6fO3/1

>>> import re
>>> m = re.search('/(Rev\d+)/[^/]+$', 'svn-backup-test,2014/09/24/18/Rev1223/FullSvnCheckout.tgz')
>>> m.groups()[0]
'Rev1223'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM