Solved

Filtering files with regular expression

Posted on 2003-10-21
4
337 Views
Last Modified: 2006-11-17
I'm trying to filter files in a directory.
To do so, I need a function using regular expression that can accept 3 parameters:
a filename, a prefix and an extension and returns true (match) or false (no match)

My code at this moment is:

<?php
function CheckPattern($filename, $prefix, $ext) {
  $testExp = "^".$prefix."([\.\+\-\@\w\d])*\.".$ext."$";
  return ($testExp, $filename) );
}
$sPref = "ABC";
$sExt = "xml";
$file = "ABC-123asq.xml";
 if (CheckPattern($file, $sPref, $sExt)) {
     echo "Its a match";
 } else {
     echo "No match";
 }
?>

It should return a match in this example, but it doesn't :-(
I'm stucked on this.

Any help?
0
Comment
Question by:itamar
  • 2
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
ThG earned 125 total points
Comment Utility

function CheckPattern($filename, $prefix, $ext) {
 $testExp = "/^".$prefix."([\.\+\-\@\w\d])*\.".$ext."$/";
 return preg_match($testExp, $filename);
}

this is your fixed function *BUT* it's very bad coding, because regexp have many metacharacters, so inserting a raw parameter into the regular expression might cause unexpected behaviour.
Guess what would happen if you enforce suffix:
".jpg"
that would match "Ajpg", "5jpg", "?jpg"
0
 
LVL 4

Author Comment

by:itamar
Comment Utility
Hi ThG,

I got your point.
But it is part of a bigger context.
Extensions are choosen from a list and prefix probably will be letters from A to Z (index)

Many thanks.

Itamar
0
 
LVL 4

Author Comment

by:itamar
Comment Utility
Hi again,

looking at your code I could see the difference is in "/^" and "$/"

As I'm PHP beginner could you explain why it's necessary as a bonus ;-))

Thanks again
0
 
LVL 14

Expert Comment

by:ThG
Comment Utility

OK :-)
That's not php related, that's the basic syntax for regular expressions. A regular expression is anclosed between two delimiters, no matter which they are, but you SHOULD use "/"
you were probably getting an error related to missing delimiter "^", because it thought you were using "^" and "^" as delimiters, which is totally wrong.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now