Regex

De WikiMar
Dreceres ràpides: navegació, cerca
/text/   Case sensible
/text/i   Not Case sensible
[^>] Any character except >
? lazy search (as small as possible)
:.+?: any amount of character between : and : (but at least one)
:.*?: any amount of character between : and : ("::" is ok)
:[^:]: should be the same
() Exports data to $match[1], second () to $match[2] ...etc.
$match[0] is everything

There are 11 characters with special meanings (have to be escaped with "\"):

the opening square bracket [
the backslash \
the caret ^
the dollar sign $
the period or dot .
the vertical bar or pipe symbol |
the question mark ?
the asterisk or star *
the plus sign +
the opening round bracket (
and the closing round bracket )


preg_match_all('/\[(.*?) (.*?)\].*?: (.*?);.*?idle=(.*?)% user=(.*?)% system=(.*?)% iowait=(.*?)%/i', $text, $match);

for ($i = 0; $i < count($match[1]); $i ++)
{
 $file = "nagiossnap/" . $match[3][$i] . ".txt";
 file_put_contents($file, $match[1][$i] . "\t" . $match[2][$i]. "\t" . $match[4][$i]. "\t" . $match[5][$i] . "\t" . $match[6][$i] . "\t" . $match[7][$i] . "\n", FILE_APPEND | LOCK_EX);

 print $match[1][$i];
 print $match[2][$i];
 print $match[3][$i];
 print $match[4][$i];
 print $match[5][$i];
 print $match[6][$i];
 print $match[7][$i];

(...)

Example taken form here: http://esa.espai.de/Logica_client-logica


Nice reference guide here: http://www.regular-expressions.info/reference.html