Click here to Skip to main content
12,999,417 members (85,734 online)
Rate this:
Please Sign up or sign in to vote.
See more: , +
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
Updated 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 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.....
Is your problem solved?
Naourass Derouichi 8-May-13 10:20am
No! i'm still searching for the solution :s
What are the problems?
Naourass Derouichi 8-May-13 10:36am
problem resolved :D
Great, congratulations. Will you accept this answer formally (green button)? I've really found some bugs for you...
Naourass Derouichi 8-May-13 10:42am
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
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170624.1 | Last Updated 8 May 2013
Copyright © CodeProject, 1999-2017
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