2017-04-05 59 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']