Click here to Skip to main content
Rate this: bad
good
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()
        {
            InitializeComponent();
 
            textBox1.Text = "http://www.site.com/Documents/file.pdf";
 
            Match matchPdf = Regex.Match(textBox1.Text, @"\*+(.pdf|.PDF)$/",
                RegexOptions.IgnoreCase);
            Match matchImg = Regex.Match(textBox1.Text, @"\*+(.jpeg|.JPEG|.gif|.GIF| .png|.PNG)$/",
                RegexOptions.IgnoreCase);
 
            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 20:20pm
Nnorss521
Edited 8-May-13 5:13am
v2

1 solution

Rate this: bad
good
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.
 
[EDIT]
 
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.
 
[END EDIT]
 
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.)
 
—SA
  Permalink  
v2
Comments
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?
—SA
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?
—SA
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...
—SA
Naourass Derouichi at 8-May-13 10:42am
   
sure!
Sergey Alexandrovich Kryukov at 8-May-13 10:44am
   
Thanks. Good luck, call again.
—SA

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

  Print Answers RSS
0 OriginalGriff 515
1 Maciej Los 290
2 Richard MacCutchan 265
3 BillWoodruff 235
4 Suraj Sahoo | Coding Passion 175
0 OriginalGriff 8,804
1 Sergey Alexandrovich Kryukov 7,457
2 DamithSL 5,689
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411028.1 | Last Updated 8 May 2013
Copyright © CodeProject, 1999-2014
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