Regex: diferència entre les revisions

De WikiMar
Salta a la navegació Salta a la cerca
(Es crea la pàgina amb « /text/ Sensible a majuscules /text/i Insensible a majuscules [^>] Any character except > ? lazy search (as small as possible) :.+?: any amount of character bet...».)
 
Cap resum de modificació
Línia 1: Línia 1:
  /text/  Sensible a majuscules
  /text/  Sensible a majuscules
  /text/i  Insensible a majuscules
  /text/i  Insensible a majuscules
Línia 9: Línia 8:
  :[^:]: should be the same
  :[^:]: should be the same


  () Exports data to $match[1], second () to $match[2]
  () Exports data to $match[1], second () to $match[2] ...etc.
  $match[0] is everything
  $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 )


<pre>
<pre>

Revisió del 20:06, 16 gen 2013

/text/   Sensible a majuscules
/text/i   Insensible a majuscules
[^>] 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