Click here to Skip to main content
11,704,509 members (58,619 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# Visual-Studio , +
hello, i have a form with a textBox and a label, i want to check if the textBox Text is finishing by ".pdf" or ".jpg", then i want to show the extention in the label :

public Form1()
            textBox1.Text = "";
            Match matchPdf = Regex.Match(textBox1.Text, @"\*+(.pdf|.PDF)$/",
            Match matchImg = Regex.Match(textBox1.Text, @"\*+(.jpeg|.JPEG|.gif|.GIF| .png|.PNG)$/",
            if (matchPdf.Success)
                label1.Text = "PDF";
            else if (matchImg.Success)
                label1.Text = "IMG";
            else label1.Text = "UNIVERSAL";

The label is always showing "Universal"... someone can help me please!
Posted 7-May-13 19:20pm
Edited 8-May-13 4:13am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I can see at least one bug: at the very beginning, use '.+', not '\*+'. Just think about it: you are not looking for a wildcard, but for some file characters, and '*' cannot ever appear in a file name.


Besides, you probably would need one more file naming scheme: .JPG. One more problem: you are not taking into account the names like *.GiF, *.GIf, *.Gif etc., as some user may legitimately use them. That said, you should not try to list all the cases, but rather need to use a case-insensitive Regex.


This Regex would not be comprehensive though: you should also check up for correct URI scheme, exclude the characters not allowed in paths, etc. Are you sure you the you want to validate it at all? Perhaps you could use one of the file dialogs? (I don't know, because I don't know your application.)

Naourass Derouichi at 8-May-13 10:08am
thanks for your attention, it is the first time that i use regex, i really need to validate the extention to tell the program what to do if its, pdf, what to do if it's jpeg.....
Sergey Alexandrovich Kryukov at 8-May-13 10:17am
Is your problem solved?
Naourass Derouichi at 8-May-13 10:20am
No! i'm still searching for the solution :s
Sergey Alexandrovich Kryukov at 8-May-13 10:32am
What are the problems?
Naourass Derouichi at 8-May-13 10:36am
problem resolved :D
Sergey Alexandrovich Kryukov at 8-May-13 10:41am
Great, congratulations. Will you accept this answer formally (green button)? I've really found some bugs for you...
Naourass Derouichi at 8-May-13 10:42am
Sergey Alexandrovich Kryukov at 8-May-13 10:44am
Thanks. Good luck, call again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 738
1 OriginalGriff 495
2 Andy Lanng 275
3 CPallini 242
4 Maciej Los 235
0 OriginalGriff 8,824
1 Sergey Alexandrovich Kryukov 8,247
2 CPallini 5,189
3 Maciej Los 4,726
4 Mika Wendelius 3,606

Advertise | Privacy | Mobile
Web01 | 2.8.150819.1 | Last Updated 8 May 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100