[英]Python Regex re.search to list
I have some code to parse the linux 'df -h', the normal command line output looks like this: 我有一些代码来解析linux'df -h',正常的命令行输出如下所示:
Filesystem Size Used Avail Use% Mounted on
udev 987M 0 987M 0% /dev
tmpfs 201M 9.2M 191M 5% /run
/dev/sda1 38G 11G 25G 30% /
tmpfs 1001M 416K 1000M 1% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 1001M 0 1001M 0% /sys/fs/cgroup
tmpfs 201M 28K 201M 1% /run/user/132
tmpfs 201M 28K 201M 1% /run/user/0
Currently my code achieves the desired output: 目前我的代码实现了所需的输出:
['/run', '/run/lock', '/run/user/132', '/run/user/0']
But the 'print ([x.split(" ")[-1] for x in newlist])' line shown below feels like a hack, I'm struggling to get this working as a regex using 'r.search' below, can anyone advise a better way of doing this please ? 但是下面显示的'print([x.split(“”)[ - 1] for x in newlist])'行感觉就像一个黑客,我正在努力使这个作为正则表达式使用下面的'r.search' ,有人可以建议一个更好的方法吗?
import subprocess
import re
cmd = 'df -h'
output = subprocess.check_output(cmd, shell=True).decode('utf8')
ln = output.split('\n')
r = re.compile('/run.*')
newlist = list(filter(r.search, ln))
print ([x.split(" ")[-1] for x in newlist])
Edit * I am using 'df -h' as some random output to practice regex on, so while @romanPerekhrest offers the best real world solution for this problem I was looking for a regex solution. 编辑*我使用'df -h'作为一些随机输出来练习正则表达式,所以虽然@romanPerekhrest为这个问题提供了最好的现实世界解决方案,但我正在寻找一个正则表达式解决方案。
The fastest approach: 最快的方法:
df -h --output=target | grep '/run.*'
The output: 输出:
/run
/run/lock
/run/user/132
/run/user/0
--output=target
- to output only mount points --output=target
- 仅输出挂载点 how about 怎么样
re.findall(r'/run.*$', output, re.MULTILINE)
I don't know about better or speed, but it cuts your code down to 3 lines, and you're regexing anyway. 我不知道更好还是速度,但是它会将你的代码减少到3行,而你无论如何都要重复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.