vorsicht t vorsicht t - 16 days ago 4
Python Question

how to write a regex to parse refs/changes

I have the following as input. I am trying to write a regular expression which yields the below output. Can anyone provide
input on how to do this?

INPUT:-

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

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

Answer

If that is the actual import format, a regex is not needed:

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

You can also have them all in one string, like this:

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