Reputation: 25
I have some question about using regular expression re
If I want to print processor time value 0.394519574284646
only in following contents
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
It should try to find words after processor time :
? Also, how to handle the space lines?
Upvotes: 0
Views: 49
Reputation: 13372
Here is the regex that will take care of space & new lines as well -
r'processor\s+time\s*:\s*(\d+(?:\.\d+)?)'
Sample run -
>>> 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']
Upvotes: 1