Skip to main content
 首页 » 编程设计

python之如何用命名组在 Python 中编写正则表达式来匹配这个

2025年05月04日2752php

我有一个包含以下行的文件。

comm=adbd pid=11108 prio=120 success=1 target_cpu=001

我编写了以下正则表达式来匹配。

_sched_wakeup_pattern = re.compile(r""" 
comm=(?P<next_comm>.+?) 
\spid=(?P<next_pid>\d+) 
\sprio=(?P<next_prio>\d+) 
\ssuccess=(?P<success>\d) 
\starget_cpu=(?P<target_cpu>\d+) 
""", re.VERBOSE) 

但现在我也有像下面这样的行,其中不存在成功组件。

comm=rcu_preempt pid=7 prio=120 target_cpu=007

如何在此处修改我的正则表达式以匹配这两种情况?我尝试在包含“成功”的行中的任何地方放置一个 *,但它会引发错误。

请您参考如下方法:

使用正则表达式非捕获组和regex.findall函数的解决方案:

import regex 
... 
fh = open('lines.txt', 'r');  // considering 'lines.txt' is your initial file 
commlines = fh.read() 
 
_sched_wakeup_pattern = regex.compile(r""" 
comm=(?P<next_comm>[\S]+?) 
\spid=(?P<next_pid>\d+) 
\sprio=(?P<next_prio>\d+) 
(?:\ssuccess=)?(?P<success>\d)? 
\starget_cpu=(?P<target_cpu>\d+) 
""", regex.VERBOSE) 
 
result = regex.findall(_sched_wakeup_pattern, commlines) 
 
template = "{0:15}|{1:10}|{2:9}|{3:7}|{4:10}" # column widths 
print(template.format("next_comm", "next_pid", "next_prio", "success", "target_cpu")) # header 
 
for t in result: 
    print(template.format(*t)) 

美化输出:

next_comm      |next_pid  |next_prio|success|target_cpu 
rcu_preempt    |7         |120      |       |007        
kworker/u16:2  |73        |120      |       |006        
kworker/u16:4  |364       |120      |       |005        
adbd           |11108     |120      |1      |001        
kworker/1:1    |16625     |120      |1      |001        
rcu_preempt    |7         |120      |1      |002