简体   繁体   English

python regex从字符串的末尾查找字符

[英]python regex find characters from and end of the string

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

from the following string I need to fetch Rev1233. 从以下字符串中,我需要获取Rev1233。 So i was wondering if we can have any regexpression to do that. 所以我想知道我们是否可以使用任何正则表达式来做到这一点。 I like to do following string.search ("Rev" uptill next /) 我喜欢执行以下string.search(“ Rev”下一个/)

so far I split this using split array s1,s2,s3,s4,s5 = string ("/",4) 到目前为止,我使用拆分数组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 这是一个快速的python示例

>>> 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 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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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