Page 1 of 1

Regular Expression with Preg_match

Posted: Wed Apr 18, 2007 10:28 pm
by winsonlee

Code: Select all

$pcode = 'LVG813';
$result[0] = 'uploads/LVG813x.jpg';
$result[1] = 'uploads/LVG813-1.jpg';
$result[2] = 'uploads/LVG813-2.jpg';
$result[3] = 'uploads/LVG813-3.jpg';
$result[4] = 'uploads/LVG813-4.jpg';



          			 for($z = 0; $z <= sizeof($results); $z++){
              			if(preg_match('/(.*?)' . preg_quote($pcode, '/').'(.*?)\\.(.*?)/i',$results[$z])){
              			 echo $results[$z];
              			}
								 }
how can i create a regular expression that only pulls out file name with '-' followed by number ?

The current regular expression only pulls out the file name in first array.

Posted: Wed Apr 18, 2007 11:54 pm
by Kieran Huggins
try:

Code: Select all

(.+?-[a-zA-Z\.]{1,})
play: http://www.cuneytyilmaz.com/prog/jrx/

Posted: Thu Apr 19, 2007 4:18 am
by stereofrog
I'd say

Code: Select all

$pcode = 'LVG813';
$result[0] = 'uploads/LVG813x.jpg';
$result[1] = 'uploads/LVG813-1.jpg';
$result[2] = 'uploads/LVG813-2.jpg';
$result[3] = 'uploads/LVG813-3.jpg';
$result[4] = 'uploads/LVG813-4.jpg';

$pcode = preg_quote($pcode);
$with_numbers = preg_grep(
	"/$pcode-\d/",
	$result);

print_r($with_numbers);

Posted: Thu Apr 19, 2007 9:45 am
by pickle
This should be in the regex forum, so I moved it.