I suck at regex. I have a php function that will read through a directory of filenames on my server, and I am trying to pull out filename data from one specific filename, but am failing miserably. Here is what I have thus far:
$file = 'VERSION_3.7.1b.txt';
$VersionNumber = preg_match('#^(VERSION_)[^\s]*\.\.[^\s]+(\.(txt))#', $file);
I am trying to have the preg_match only return true for files that begin with ‘VERSION_’ and end with ‘.txt. The stuff in the middle may be any variety of numbers, letters, periods…and that is the part I want to return. So in this case, I want $VersionNumber to end up set as ‘3.7.1b’
Other filenames that would return something are like these:
VERSION_3.txt -> 3
VERSION_3.7.txt -> 3.7
VERSION_3.7e.1.txt -> 3.7e.1
VERSION_2012.09.13.3.7.1b.txt -> 2012.09.13.3.7.1b
VERSION_3.7.20120913.txt -> 3.7.20120913
Any regex masters out there that can explain to me if I am even close? Thanks!
You’ll want to pass a variable for $matches as well.
I simplified your RegExp. Try this: