小编Vip*_*pul的帖子

Python中的复杂正则表达式

我试图使用正则表达式编写一个通用模式,以便它只从字符串中获取特定的东西.假设我们有像GigabitEthernet0/0/0/0或FastEthernet0/4或Ethernet0/0.222这样的字符串.正则表达式应该获取前2个字符和所有数字.因此,取决于上述情况,取出的结果应该是Gi0000或Fa04或Et00222.

x = 'GigabitEthernet0/0/0/2
m = re.search('([\w+]{2}?)[\\\.(\d+)]{0,}',x)
Run Code Online (Sandbox Code Playgroud)

我无法理解如何编写正则表达式.也可以以列表的形式获取值.我写了更多的模式,但它没有帮助.

python regex

0
推荐指数
1
解决办法
81
查看次数

如果子程序命令中没有shell = True,则无法运行命令

我正在Linux中使用python开发一个小型工具。之前我使用的是Python 2.7,但现在将其更改为Python 3.4,以查看它是否可以帮助解决我的问题。当我给出以下代码时:

try:
    x=subprocess.check_output(command, shell=True, timeout=3)
except subprocess.TimeoutExpired as exc:
    print ("Timeout bro")
    exit()
except Exception as e:
    msg = "Some issues in fetching details"
    print (msg)
Run Code Online (Sandbox Code Playgroud)

由于该命令从另一台设备获取详细信息,并且该设备无法正常运行,因此3秒钟后超时,并显示消息“ Timeout bro”。我阅读了使用shell = True的安全性问题,因此使它一次成为shell = False,第二次我删除了该参数。

try:
    x=subprocess.check_output(command, shell=False, timeout=3)
except subprocess.TimeoutExpired as exc:
    print ("Timeout bro")
    exit()
except Exception as e:
    msg = "Some issues in fetching details"
    print (msg)
Run Code Online (Sandbox Code Playgroud)

我在不同的地方都读到该命令在shell = False上同样有效。但是,只要我在代码中运行上述代码,shell=False就无需等待3秒钟即可直接打印“获取详细信息中的某些问题”。有没有办法可以在没有shell = True的情况下运行代码?请帮忙。谢谢!

python subprocess python-3.x

-1
推荐指数
1
解决办法
1151
查看次数

标签 统计

python ×2

python-3.x ×1

regex ×1

subprocess ×1