« Septiembre 2004 | Main | Noviembre 2004 »

Octubre 2004 Archives

Octubre 23, 2004

Expresiones regulares en php con preg_match

He tardado un buuuen rato en encontrar como hacer una expresion
regular en php. El problema es que con "ereg" no permite
que sean "non-greedy", y hay que utilzar la familia preg_*
Este ejemplo me ayudó bastante (en aleman):

$string = 'Dieser Satz wird fast gefressen';

// Normalzustand:
preg_match('/D.*r/', $string, $matches);
-> Dieser Satz wird fast gefr

// Einen Quantifier ungreedy gemacht:
preg_match('/D.*?r/', $string, $matches);
-> Dieser

// Greediness aller Quantifier umgeschaltet:
preg_match('/D.*r/U', $string, $matches);
-> Dieser

// Doppelt gemoppelt: Greediness umgeschaltet,
// einen Quantifier wieder zurückgeschaltet:
preg_match('/D.*?r/U', $string, $matches);
-> Dieser Satz wird fast gefr

// Bei dieser Schreibweise spielt die Greediness keine Rolle:
preg_match('/D[^r]*r/', $string, $matches);
-> Dieser

About Octubre 2004

This page contains all entries posted to Alejandro Ramos :: personal webpage in Octubre 2004. They are listed from oldest to newest.

Septiembre 2004 is the previous archive.

Noviembre 2004 is the next archive.

Many more can be found on the main index page or by looking through the archives.

rss
unsec dot net