检查包含文件路径的字符串中的扩展名 [英] checking extention in a string that contain a file path

查看:71
本文介绍了检查包含文件路径的字符串中的扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有一个带有textBox和标签的表单,我想检查textBox文本是否以.pdf或.jpg结束,然后我想在标签中显示扩展名:



  public  Form1()
{
InitializeComponent ();

textBox1.Text = http://www.site.com/Documents/file .PDF;

匹配matchPdf = Regex.Match(textBox1.Text, @ \ * +( .pdf | .PDF)$ /
RegexOptions.IgnoreCase);
匹配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;
}







标签总是显示通用...有人可以提供帮助我请

解决方案
/,
RegexOptions.IgnoreCase);!
匹配matchImg = Regex.Match(textBox1.Text , @ \ * +(。jpeg | .JPEG | .gif | .GIF | .png | .PNG)

/
RegexOptions.IgnoreCase);

<跨度类= 代码关键字>如果 (matchPdf.Success)
{
label1.Text = PDF;
}
else if (matchImg.Success)
{
label1.Text = IMG;
}
else label1.Text = UNIVERSAL;
}







标签总是显示通用...有人可以提供帮助我请!


我至少可以看到一个错误:在一开始,使用''。+'',而不是''\ * +''。试想一下:你不是在寻找通配符,但是对于某些文件字符,*不能出现在文件名中。







此外,您可能还需要一个文件命名方案:.JPG。还有一个问题:您没有考虑* .GiF,* .GIf,*。Gif等名称,因为某些用户可能合法地使用它们。也就是说,你不应该尝试列出所有的情况,而是需要使用不区分大小写的正则表达式。



[结束编辑]



这个正则表达式并不全面:你还应检查正确的 URI方案,排除路径中不允许的字符等。你确定你是你想验证它吗?也许您可以使用其中一个文件对话框? (我不知道,因为我不知道你的申请。)



-SA


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!

解决方案

/", 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!


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


这篇关于检查包含文件路径的字符串中的扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆