Page 1 of 1
preg_match help
Posted: Thu Sep 18, 2003 2:23 am
by yaron
Hi,
I'm cracking my head with the preg_match function.
my regular expressions is not very strong but here is want I need:
I have a file name that can come in 2 different forms:
1. ty-08fileName.txt - i.e. 2 letters (not digits or other chars) follwed by a "-" and then 2 digits and then the actual file name.
2. fileName.txt - just a regular file name
What I need to do is to find out if the file name appears in the first option
and if so to split those 2 strings e.g. 1-ty-08 2-fileName.txt
and if it's in the second option just do nothing.
I tried a couple of expressions but non of them worked for all cases.
any ideas?
Thank you in advance
Posted: Thu Sep 18, 2003 6:05 am
by JAM
I prefer string functions.
Code: Select all
if (strstr($filename, '-')) {
// found a -
echo substr($filename,5);
} else {
echo $filename;
}
yes but,
Posted: Thu Sep 18, 2003 6:09 am
by yaron
I have to find the exact pattern of ww-dd because the filename may contain "-" as well...
Posted: Thu Sep 18, 2003 6:27 am
by volka
try
Code: Select all
<?php
$subjects = array(
'ty-08fileName.txt',
'fileName.txt',
't1-45fileName.txt',
'ty--08fileName.txt',
'ty-8fileName.txt',
'something-ty-08fileName.txt',
);
$pattern = '!^([[]]{2}-\d{2})(.*)$!';
foreach($subjects as $s)
{
if(preg_match($pattern, $s, $matches) > 0)
{
echo "match\n";
print_r($matches);
}
else
echo "no match: $s\n";
}
?>
Posted: Thu Sep 18, 2003 2:50 pm
by m3rajk
volka: i don't thinkg one can use posix sets in perl.
volka: yours must have the letters and digits (assuming the posix set works) and you don't need the () around .*
otherwise his would be good. you can't use \w becasue \w is [a-zA-Z0-9_]
so so your pattern should be
Code: Select all
$pattern='|^([A-Za-z]{2}-\d\d)?.+$|';
^ means that starts the string and $ means that the pattern goes tot he end
[A-Za-z] is any letter
{2} means it occurse 2 times
- is a dash
\d\d is simpler than \d{2} and does the same
putting that in () with a ? makes it optional
.+ is any number of occurances of any character (doesn't need to be the same character)
the problem is that will match ANYTHING due to the .+
note that i used + where he used * this is becasue .* would make the pattern match an empty string, + means that the empty string is the ONLY thing that wont match that pattern i gave
if the files only use characters in \w then
Code: Select all
$pattern='|^([A-Za-z]{2}-\d\d)?\w+\.\w+$|';
that means the file name an extention can be any number of characters in the \w range
Posted: Thu Sep 18, 2003 6:40 pm
by volka
m3rajk: If I post a code snippet <?php ... ?> it's usually testable. Don't think, try

[:alpha:] is suported and matches [A-Za-z]
And yes, either it's matching, then it enters the true-block printing the elements, or it does not match and prints the filename as is. I don't like fetching optional subpatterns if it can be avoided
Posted: Fri Sep 19, 2003 1:34 pm
by m3rajk
your .* means the simple prefix will work
my .+ needs more thant he prefix.
i still think hs whould look at valid characters and limits for the file names , and if possible use something like \w{1,9}\.\w\w((\w)?\w)? which would give 1 to 9 \w characters for the filename and 2 to 4 \w characters for the file type