-3

The following code does not work all the time. I have looked at countless regex examples but very few address the use of multiple extensions.

public bool FAQPNFileCheck(string name)
{
    if (name.Length > 0)
    {

        Match match = Regex.Match(name, 
                                  @"\\([A-Za-z0-9_-]+)\.(jpg|doc|pdf)$", 
                                  RegexOptions.IgnoreCase);

        // Here we check the Match instance.
        if (match.Success)
        {
            // Finally, we get the Group value and display it.
            string key = match.Groups[1].Value;
            return true;
            //Console.WriteLine(key);
        }

    }
    if (name == "")
    {
        return true;
    }

    return false;
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
InCode
  • 503
  • 3
  • 8
  • 25

3 Answers3

1

If you are looking for something like this: this_is_not_a_picture.jpg.doc, as Andre asked, you are not allowing for a literal dot (.) in your regex until the end.

This should do it:

\\([A-Za-z0-9._-]+)\.(jpg|doc|pdf)$

Mark Avenius
  • 13,679
  • 6
  • 42
  • 50
  • No Just a normal extension...but different extension types like this: this_is_a_pic.jpg or this_is_a_doc.doc but not this is wrong._ff (contains spaces and wrong extension...) – InCode Aug 20 '12 at 19:10
  • @ENC0D3D So you want *any* extension? What determines an extension, a dot + 3 characters? – Andre Calil Aug 20 '12 at 19:15
  • @AndreCalil And keep in mind not all file extensions are 3 chars either. Some are 2, or 4, or 5 or 10, or 1 or ... – Servy Aug 20 '12 at 19:17
  • @Servy Indeed, that's why I'm asking what *he* accepts as a file extension – Andre Calil Aug 20 '12 at 19:17
  • @ENC0D3D That doesn't really answer the question. Do you want to allow any possible file extension? If so, it pretty much needs to be anything after the last dot. – Servy Aug 20 '12 at 19:19
  • 4
    @ENC0D3D OMG dude, seriously?! We're trying to help you because you can't even explain yourself and you post a link to Wikipedia, like if *we* didn't know what a file extension is?! Should you link Wikipedia's Regex entry for you, so? – Andre Calil Aug 20 '12 at 19:19
  • I apologize but I want to only accept only those types of extensions/filetypes.....jpg pdf or doc only.. – InCode Aug 20 '12 at 19:24
  • 1
    Oh my, now I see why your username is *encoded*. I simply can't understand what you're looking for. – Andre Calil Aug 20 '12 at 19:25
  • @ENC0D3D No dude, you're getting flamed for not explaining yourself. Let's try again: you want to match **any** file extension or only those 3? – Andre Calil Aug 20 '12 at 20:46
  • That's it...But the other caveat is I don't want to allow just any character for the filename as well.....(Thats why I didn't include the period in the original posting...) – InCode Aug 20 '12 at 20:52
0

Try from RightToLeft

Regex r=new Regex(@"([A-Za-z0-9_-]+)\.(jpg|doc|pdf)$",RegexOptions.RightToLeft);
Anirudha
  • 32,393
  • 7
  • 68
  • 89
  • Just to elaborate on this solution. The engine still works from left to right, it just tries to match the `$` at the end of the string. The engine does in no way reverse. Assuming it works from Right to Left with the `$` anchor may have adverse effects. – Firas Dib Aug 20 '12 at 19:20
  • @Anirudha Please see comments above. Thank you. – InCode Aug 20 '12 at 19:24
  • @Lindrian any proof to back your thought – Anirudha Aug 20 '12 at 19:27
  • @Lindrian here's a food for ur thought! If u want to match these:`jpg.jpg` OR `pdf.pdf`,the regex `^\1\.(jpg|doc|pdf)$` would only work with `RightToLeft` option..So it does in reverse way.. – Anirudha Aug 20 '12 at 19:40
  • I want to only allow three file types: jpg pdf or doc...no spaces are allowed for filename just the characters within the [ ]. I can't believe this is so difficult..... None of the suggestions above have worked... Thanks for your help. – InCode Aug 20 '12 at 19:47
  • @Anirudha: What? You are wrong and that expression is wrong. That expression matches an octal escape of chr(1) and then whatever you want it to match. You are just plain wrong on this one. – Firas Dib Aug 20 '12 at 20:31
  • @Lindrian **octal escape of chr(1)** lol...`\1` is a reference to first captured group...Plz..haha – Anirudha Aug 21 '12 at 03:27
  • @Anirudha: clearly your regex skills are severely limited. I am not going to bother explaining. You are wrong, stop giving out false information. – Firas Dib Aug 21 '12 at 07:00
-1

Ok, so after all, you want to allow files with the extesions jpg, doc or pdf, right?

Let's try this:

Regex.Match(name, @"^(?i:[A-Z0-9\_\-]+)\.(?i:jpg|doc|pdf)$", RegexOptions.Compiled);

As latkin pointed out, if you're going to use this Regex object once, then RegexOptions.Compiled is not a good choice, because it will take longer to instantiate the object. However, the match will run faster, so it's a good idea to keep it if you're going to use it on several files (as I was supposing), then keep it at a Regex instance.

Andre Calil
  • 7,652
  • 34
  • 41
  • 1
    You can use Compiled option with or without ?i. And there is no point in including both A-Z and a-z if you are doing a case-insensitive match. – latkin Aug 20 '12 at 19:14
  • @latkin I just let the default OP regex. Besides, if you are not using `(?i:)` but you want a case-insensitive search, then you have to use the option for it, so you can't choose `compiled`. Got it? – Andre Calil Aug 20 '12 at 19:16
  • 1
    Also, using the Compiled option make the match itself faster, but the up-front cost of the compilation is large. So if you are only doing a 1-off match, it will actually be MUCH slower to Compile. It is only smart to do if you will use the same regex for a large number of matches. See http://stackoverflow.com/a/7707369/1366219 – latkin Aug 20 '12 at 19:18
  • Compilation time x runtime. Which one do I want to be faster under any circunstance? Hm, ok, runtime. Thanks for the link, anyway. – Andre Calil Aug 20 '12 at 19:20
  • 2
    RegexOptions are bitwise flags. You can combine them: RegexOptions.IgnoreCase | RegexOptions.Compiled It's not like you have to choose just 1 options. – latkin Aug 20 '12 at 19:20
  • I don't think you understand... Your regex is not compiled when your C# is compiled. It is compiled AT RUNTIME by the Regex engine. This is a perf hit AT RUNTIME. It makes sense if you are using the same regex object to match against lots of strings. It does not make sense for 1-time matches, as explained in detail by the link I provided. – latkin Aug 20 '12 at 19:23
  • @latkin Ok, now think a little bit broader: it's a regex to match a filename. Do you really think it'll only be used once? I don't think so. – Andre Calil Aug 20 '12 at 19:26