I have thousands of files in a directory that i am running a batch script on to process.
The relevant files all start with XX-XX-XX (at least one integer, but no more than two, with underscores separating the three numbers)
I used txt2re to create this:
$txt='1-2-3';
$re1='(\\d+)'; # Integer Number 1
$re2='(-)'; # Any Single Character 1
$re3='(\\d+)'; # Integer Number 2
$re4='(-)'; # Any Single Character 2
$re5='(\\d+)'; # Integer Number 3
if ($c=preg_match_all ("/".$re1.$re2.$re3.$re4.$re5."/is", $txt, $matches))
{
$int1=$matches[1][0];
$c1=$matches[2][0];
$int2=$matches[3][0];
$c2=$matches[4][0];
$int3=$matches[5][0];
print "($int1) ($c1) ($int2) ($c2) ($int3)
";
}
Is it possible to get a slightly more compact version of this, that I can compare to the filename in a single function.
You could use
"/^\d\d?-\d\d?-\d\d?/"
If the digits are separated by underscores use _
instead of -
.
You can concatenate everything together at declaration.
$txt='1-2-3';
$re = '/(\d+)-(\d+)-(\d+)/is';
if ($c=preg_match_all ($re, $txt, $matches))
{
$int1=$matches[1][0];
$int2=$matches[2][0];
$int3=$matches[3][0];
print "($int1) (-) ($int2) (-) ($int3)
";
}
Note that I removed the grouping around the -
since those stay constant. You can walk through this regex step-by-step at Debuggex.