I’m using the following regular expression with the preg_match function in PHP to match the output of the df command in Linux. Note that I used a define macro to simplify the readability of the regular expression.
define("MATCH_DF",
"/^(?P<filesystem>[0-9a-z\/]+)\s+" .
"(?P<blocks>[0-9]+)\s+" .
"(?P<used>[0-9]+)\s+" .
"(?P<avail>[0-9]+)\s+" .
"(?P<percent>[0-9]+%)\s+" .
"(?P<mount>[0-9a-zA-Z\/]+)\s+$/");
For those who do not know, the output of the df command line looks like this…
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 236520176 2863468 221642116 2% /
Can anyone tell me what I’m missing here? Why isn’t this working? I’ve narrowed the problem down to being something in my regular expression.
You are looking for some whitespace at the end of the string, of which there isn’t any. You could swap the
+quantifier for a*to make it optional, or remove it all together.Try this regex…
CodePad.
I also swapped your
[0-9]for\d.If you want to discard the numeric matches and just keep the named capturing groups…
CodePad.