Reputation: 6150
I am trying to make a list of all PHP files within a directory that have a specific character string (in my case a datestamp) at the beginning of the name AND a specific string ".php" at the end. My date is being passed in as a variable ($SearchDate). I have already successfully set up the PHP script to read in a full list of files from my directory, and was just trying to use preg_match to filter the list for me. Unfortunatly I am a complete failure at RegEx even having tried repeatedly with this cheatsheet.
Here is what I have that is generating errors and no results:
$SearchDate = '2013-02'; //example for february of 2013
if(preg_match('#^('.$SearchDate.')+[:graph:]{1}(\.(pdf))#', $file)) {
//do something
}
I also tried:
preg_match('#^\Q'.$SearchDate.'\E+[:graph:]{1}\Q.pdf\E#', $file)
My filenames look like this:
2013-02-fileNameMightBeAlphaOr1234567890.pdf
The errors I am getting look like this:
Warning: preg_match() [function.preg-match]: Compilation failed: POSIX named classes are supported only within a class at offset 5 in /home/directory/myfile.php on line 26
Upvotes: 1
Views: 2072
Reputation: 786291
I need to also be checking that it ends with ".pdf" so I basically need the start and end of the filename and don't care what happens in the middle.
If that's the case then following regex should work for you:
if(preg_match('#^' . $SearchDate . '.*?\.pdf$#', $file)) {
//do something
}
Upvotes: 2
Reputation: 1617
add {
and }
to your graph...like
preg_match('#^('.$SearchDate.')+[{:graph:}]{1}(\.(pdf))#', $file)
Upvotes: 0
Reputation: 4859
this simple regexp should work
preg_match('/^'.$SearchDate.'/', $file);
Upvotes: 1