Regular Expression with Preg_match

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
winsonlee
Forum Commoner
Posts: 76
Joined: Thu Dec 11, 2003 8:49 pm

Regular Expression with Preg_match

Post 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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

try:

Code: Select all

(.+?-[a-zA-Z\.]{1,})
play: http://www.cuneytyilmaz.com/prog/jrx/
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post 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);
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

This should be in the regex forum, so I moved it.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply