2017-04-05 62 views
0

我有关于使用正则表达式re 如果我想在下面的内容Python的关于正则表达式

Readings  : \\demoweb01\processor(_total)\% processor time : 

       0.394519574284646 





PSComputerName : DEMOWEB01 

RunspaceId  : 8c9e3d4b-8908-4a30-bef4-1f26d4a511bb 

Timestamp  : 4/3/2017 2:39:30 PM 

CounterSamples : {Microsoft.PowerShell.Commands.GetCounter.PerformanceCounterSa 

       mple} 



Readings  : \\demoweb01\processor(_total)\% processor time : 

       1.69883362197086 

应该尽量processor time :后用语言来打印处理器时间值仅0.394519574284646一些问题?另外,如何处理空间线?

回答

1

这里是一个会照顾空间&新线以及正则表达式 -

r'processor\s+time\s*:\s*(\d+(?:\.\d+)?)' 

采样运行 -

>>> output = """Readings  : \\demoweb01\processor(_total)\% processor time : 
... 
...     0.394519574284646 
... 
... 
... 
... 
... 
... PSComputerName : DEMOWEB01 
... 
... RunspaceId  : 8c9e3d4b-8908-4a30-bef4-1f26d4a511bb 
... 
... Timestamp  : 4/3/2017 2:39:30 PM 
... 
... CounterSamples : {Microsoft.PowerShell.Commands.GetCounter.PerformanceCounterSa 
... 
...     mple} 
... 
... 
... 
... Readings  : \\demoweb01\processor(_total)\% processor time : 
... 
...     1.69883362197086""" 
>>> import re 
>>> re.findall(r'processor\s+time\s*:\s*(\d+(?:\.\d+)?)', output) 
['0.394519574284646', '1.69883362197086']