简体   繁体   中英

regular expression to grep the matching string in python

I got one list as follows by running the command in subprocess:

import os, subprocess
>>> args = "svn ls https://svne1.access.nsn.com/isource/svnroot/scm_fp/trunk"
>>> pi = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
>>> stdout, stderr = pi.communicate()
>>> print stdout
R_FPT_180.15.1.11.1.0/
R_FPT_180.15.1.41/
R_FPT_180.15.1.42/
R_FPT_190.10/
R_FPT_190.11/
R_FPT_190.6.1.0/
R_FPT_2.4.1.22/
R_FPT_3.2.1.70/

In the above list I want to grep only "R_FPT_190.10/" and "R_FPT_190.11/" and after grepping these two again which ever is having big number in the last(in the above example 10,11) i need to get the greater one (in the above example i need to get finally "R_FPT_190.11/"). Can anyone help in this ?

Using regex:

Ex:

s = """R_FPT_180.15.1.11.1.0/
R_FPT_180.15.1.41/
R_FPT_180.15.1.42/
R_FPT_190.10/
R_FPT_190.11/
R_FPT_190.6.1.0/
R_FPT_2.4.1.22/
R_FPT_3.2.1.70/"""
import re
print(re.findall("R_FPT_\d+\.\d+\/", s))

d = max([int(i.split(".")[-1].rstrip(r"/")) for i in re.findall("R_FPT_\d+\.\d+\/", s)])
print(d)

Output:

['R_FPT_190.10/', 'R_FPT_190.11/']
11

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