I have this string:
28.6MH\/s 27.3MH\/s | Temp(C): 64 66 61 64 63 | Fan: 74% 76% 69% 75% 72% | HW: 21 21 21
and I want to extract the Temp
values, but I can't figure out what I'm doing wrong.
The expression that I came up with (and doesn't work) is:
((?<temp>\d\d)(?!\.).+(?!Fan))+
Debuggex Demo
Pattern: /(?:\G(?!^)|Temp\(C\):) \K\d+/
(Demo)
Code: (Demo)
$in='28.6MH\/s 27.3MH\/s | Temp(C): 64 66 61 64 63 | Fan: 74% 76% 69% 75% 72% | HW: 21 21 21 ';
var_export(preg_match_all('/(?:\G(?!^)|Temp\(C\):) \K\d+/',$in,$out)?$out[0]:'fail');
Output:
array (
0 => '64',
1 => '66',
2 => '61',
3 => '64',
4 => '63',
)
Explanation:
You can see the official terminology explanation in the Pattern Demo link, but here is my way of explaining...
(?: # start a non-capturing group so that regex understands the piped "alternatives"
\G # match from the start of the string or where the previous match left off
(?!^) # ...but not at the start of the string (for your case, this can actually be omitted, but it is a more trustworthy pattern with it included
| # OR
Temp\(C\): # literally match Temp(C):
) # end the non-capturing group
# <-- there is a blank space there which needs to be matched
\K # "release" previous matched characters (restart fullstring match)
\d+ # match one or more digits greedily
The pattern stops when it hits that |
("space and pipe") after 63
because they aren't matched by \d+
("space and digits").
With preg_match()
function and specific regex pattern:
$str = "28.6MH\/s 27.3MH\/s | Temp(C): 64 66 61 64 63 | Fan: 74% 76% 69% 75% 72% | HW: 21 21 21";
preg_match('/(?<=Temp\(C\): )[\s\d]+(?=\| Fan)/', $str, $m);
$temp_values = $m[0];
print_r($temp_values);
The output:
64 66 61 64 63