-3

Possible Duplicate:
How do I check if a filename matches a wildcard pattern

In the program that I am doing, the user has two inputs.

Input 1 = the filename.
Input 2 = the flagFilePattern

ex. 1
Input 1 = file1.xml or file25.xml or file_123.xml
Input 2 = file*.xml
Result (all files should match based on the pattern(Input 2)

ex. 2
Input 1 = file1.xml.done
Input 2 = file*.xml
Result (the file should not match based on the pattern, because .xml was not found at the last of the filename).

Question, what should be my regular expression based on the example above?
Note: my code is in C#

Community
  • 1
  • 1
Lemuel Nitor
  • 333
  • 4
  • 5
  • 15
  • 1
    -1 It is unclear what you want to validate, it is unclear if the input2 is dynamic, it is unclear if input2 should always be a filename with the xml-extension and it is unclear why you want to use a regex for the task rather than just validate if the file exists. – Casperah Sep 17 '12 at 06:44

3 Answers3

1

The value for input 2 can be a valid regular expression. All you need to do would be to add the ^ and $ anchors, these will allow your regex engine to specifically match that pattern, so in your case, you could do something like so:

 String input2 = "file*.xml";
        Regex regex = new Regex("^" + input2.Replace(".", "\\.").Replace("*",".*") + "$");
        String input1 = "file_123.xml";
        String input3 = "file_123.xml.done";

        System.Console.WriteLine(regex.IsMatch(input1));
        System.Console.WriteLine(regex.IsMatch(input3));
        System.Console.ReadLine();
...

The . is a special character in regex so it needs to be escaped. The second replace statement adds a . which means any character infront of the * operator which means or or more repetitions of. The code above yields True and False respectively.

EDIT: As pointed above, you will need to escape more characters depending on your scenario.

EDIT 2: The code below should take care of escaping any string which is part of the regular expression language. In your case, this also means the * operator. I used the Regex.Escape method the escape all the characters which might have a special regex meaning and then used the usual replace to get the * back on track.

            String input2 = "file*.xml";
            input2 = Regex.Escape(input2);  //Yields file\\*\\.xml
            input2 = input2.Replace("\\*", ".*");  //Yields file.*\\.xml
            Regex regex = new Regex("^" + input2 + "$");
            String input1 = "file_123.xml";
            String input3 = "file_123.xml.done";

            System.Console.WriteLine(regex.IsMatch(input1));
            System.Console.WriteLine(regex.IsMatch(input3));
            System.Console.ReadLine();
npinti
  • 51,780
  • 5
  • 72
  • 96
1

just use input2 and transform that to a correct pattern like

var regex = new Regex(@"\A" + (args[1].Replace(".", @"\.").Replace("*", ".*")) + @"\Z");
TheHe
  • 2,933
  • 18
  • 22
  • This is incomplete at best and misleading at worst. There are many other regex metacharacters that you need to escape, and you're not handling `?` at all. – Tim Pietzcker Sep 17 '12 at 06:38
  • from my point of view - he just want to use "*" as glob-selector and "?" and other metachars are not very common in normal filenames... – TheHe Sep 17 '12 at 06:39
  • in addition -- in his examples the names are clear and not very "problematic" – TheHe Sep 17 '12 at 06:39
  • 1
    `?` is a glob metacharacter. And how do you know the filenames don't contain parentheses etc.? – Tim Pietzcker Sep 17 '12 at 06:40
  • i don't know that - just provided an answer which completly matches the example above... guy - i know what you're talking about, but that's not the question. – TheHe Sep 17 '12 at 06:41
  • Nice of you to provide an answer that allows the asker to shoot himself in the foot only because he hasn't thought of the relevant test cases yet. – Tim Pietzcker Sep 17 '12 at 06:41
  • nice to shoot an replier because he hasn't thought of ALL and MAYBE not question-relevant aspects which weren't pointed out in the question ;) c'mon... – TheHe Sep 17 '12 at 06:43
  • The point of answering a question is not to provide a solution for exactly the test cases the author of the question has thought of yet, but to solve a problem. A solution like this one gives regular expressions a bad name. See the duplicate question I linked to for what a real answer should look like in my opinion. – Tim Pietzcker Sep 17 '12 at 06:47
  • we solved EXACTLY this problem and @npinti pointed out futures problems like a charm. we're not here to TOTALLY think for the authors - just solve a "problem" and let him go alone... kind of "learning"... if he will have problems again with the solution, he will ask again!? – TheHe Sep 17 '12 at 06:52
-1

if input 2 is always a valid Regex, you can use following code:

String regex = input2;

Pattern pattern = Pattern.compile(regex); 

Matcher matcher = pattern.matcher(input1); 
Azodious
  • 13,752
  • 1
  • 36
  • 71
  • Input2 isn't a valid regex in the example (well, it's valid but it doesn't work as intended). It looks rather like a glob pattern – Tim Pietzcker Sep 17 '12 at 06:34
  • Thanks for correcting. This link may help visitors: http://www.linuxquestions.org/questions/linux-newbie-8/what-is-the-difference-between-regular-expressions-and-globbing-828589/ – Azodious Sep 17 '12 at 06:48