简体   繁体   English

正则表达式grep python中的匹配字符串

[英]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/"). 在上面的列表中,我只想grep“ R_FPT_190.10 /”和“ R_FPT_190.11 /”,然后再次grep这两个在最后一个有较大编号的代码(在上面的示例10,11中),我需要获取更大的一个(在上面的示例中,我需要最终获得“ 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

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

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