如何编写正则表达式来解析引用/更改

时间:2014-11-18 19:44:28

标签: python

我有以下输入。我试图写一个正则表达式,产生以下输出。谁能提供 输入如何做到这一点?

INPUT:-

refs/changes/44/1025744/3
refs/changes/62/1025962/5 
refs/changes/45/913745/2

OUTPUT:-
1025744/3
1025962/5 
913745/2

3 个答案:

答案 0 :(得分:0)

>>> import re
>>> input="""refs/changes/44/1025744/3
... refs/changes/62/1025962/5 
... refs/changes/45/913745/2"""
>>> res=re.findall(r'.*/.*/.*/(.*/.*)',input)
>>> for i in res:
...    print i
... 
1025744/3
1025962/5 
913745/2

答案 1 :(得分:0)

如果您逐行输入输入,则可以执行以下操作:

import re
instr = 'refs/changes/44/1025744/3'
print get_match(instr)

def get_match():
    match = re.match("^(refs/changes/[0-9]*/)([0-9/]*)", instr)
    if match:
        return match.group(2)

答案 2 :(得分:0)

如果这是实际的导入格式,则不需要正则表达式:

>>> source = """\
... refs/changes/44/1025744/3
... refs/changes/62/1025962/5
... refs/changes/45/913745/2
... """
>>> output = [line.split('/', 3)[-1] for line in source.splitlines()]
>>> output[0]
'1025744/3'
>>> output[1]
'1025962/5'

您也可以将它们全部放在一个字符串中,如下所示:

>>> ' '.join(line.split('/', 3)[-1] for line in source.splitlines())
'1025744/3 1025962/5 913745/2'