carte blanche
carte blanche

Reputation: 11476

grab the latest string and print its value

I have the following code which finds all of the tag "depends-on" and joins them and prints them..now I want to grab only the last depends on and print is value..expected output and current out put is shown below.

Current output:-

['348523', '350167', '350169', '348522', '350166', '350168', '350169', '350170', '350428', '350435', '350439', '350446', '350449', '350450', '350459', '350462', '350463', '350466', '350472', '350475', '350810', '350811', '350812', '350870', '350871', '350875', '350876', '350882', '350883', '350884', '350885', '348521']

Expected output:-
['348521','238567']

Code:-

import re
def findexternaldep ():
    #print "Processing dependency for change %s", change
    #change=str(change)
    comments = ''' comments:
    timestamp: 2013-06-12 09:18:36 PDT
    reviewer:
      name: user L
      email: [email protected]
      username: username
    message: Patch Set 2:

             Depends-on: 348523 350167 350169
    timestamp: 2013-06-12 09:18:36 PDT
    reviewer:
      name: user L
      email: [email protected]
      username: username
    message: Patch Set 2:

             Depends-on: 348522 350166 350168 350169 350170 350428 350435 350439 350446 350449 350450 350459 350462 350463 350466 350472 350475 350810 350811 350812 350870 350871 350875 350876 350882 350883 350884 350885
  comments:
    timestamp: 2013-06-12 10:39:46 PDT
    reviewer:
      name: user L
      email: [email protected]
      username: username
    message: Patch Set 2:
             Depends-on: 348521 238567
    '''

    print "COMMENTS"
    print comments
    deps = ' '.join(re.findall(r'(?<=Depends-on:\s)[\d ]+(?=\n)', comments)).split()
    print "DEPS"
    print deps
    #print depgerrit
    return deps

def main ():
    findexternaldep()

if __name__ == '__main__':
    main()

Upvotes: 0

Views: 101

Answers (4)

perreal
perreal

Reputation: 97948

What about using a greedy dot match to skip until the last Depends-on:

m=re.match(r'.*Depends-on:\s*([^\n]*)', comments, re.DOTALL)
if m: print m.group(1)

Output:

348521

Upvotes: 1

Jon Clements
Jon Clements

Reputation: 142156

Just search backwards in the comments, then take up until the end of line:

print comments.rsplit('Depends-on: ', 1)[1].split('\n', 1)[0]

Or modifying your regular expression to be:

deps = re.findall(r'(?<=Depends-on:\s)[\d ]+(?=\n)', comments)

Then get the last element:

print deps[-1]

And if you wanted them all joined:

all_deps = ' '.join(deps) # similar to before

Upvotes: 1

aronchick
aronchick

Reputation: 7128

Do you mean you want it to return the last one in the list? With no error checking, wouldn't that just be:

deps[-1]

Otherwise, the issue is that each one is getting found in the regular expression, so they're all being included in the list.

Upvotes: 0

jedwards
jedwards

Reputation: 30210

If by latest, you mean the last element of the list, then you can get that by:

deps[-1]

For example:

a = [2,4,6]
e = a[-1]
print(e)    #  Prints "6"

This seems like what you are looking for by your expected output.

However, if you're looking for the largest or smallest of the list elements (when interpreted as integers), you could use something like:

max(map(int, deps))

For example:

a = ['2','4','1','3']
e = max(map(int, a))
print(e)    #  Prints "4"

Upvotes: 0

Related Questions