如何检查文件名是否包含GUID [英] How to check whether a file name contains GUID or not

查看:344
本文介绍了如何检查文件名是否包含GUID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件名是text1eb4617fd-4876-4c52-aa5b-21f8be8fb0f8.txt

它应该返回true,因为它包含GUID



如果文件名是text1eb4617fd-4876-f8.txt或text1.txt

它应该返回false,因为它不包含任何GUID

解决方案

正则表达式将是完美的:



 使用系统。文本; 

string pattern = @ ^文本(小于GUID> [0-9A-ZA-Z] {8} - [0-9A-ZA-Z] {4} - [0-9A-ZA-Z] {4} - [0 -9a-ZA-Z] {4} - [0-9A-ZA-Z] {12})\.txt

;

正则表达式regex = 正则表达式(模式,RegexOptions.Compile | RegexOptions.CultureInvariant);

string input1 = text1eb4617fd-4876-4c52-aa5b-21f8be8fb0f8.txt;
string input2 = texteb4617fd-4876- 4c52-aa5b-21f8be8fb0f8.txt;

bool input1Matches = regex.IsMatch(input1); // input1Matches将为false
bool input2Matches = regex.IsMatch(input2); // input2Matches将为true



As奖金,如果你想获得Guid,它很简单:

匹配m = regex.Match(input2); 
Guid guid = Guid.Parse(m.Groups [ Guid]。价值) ;



注意:您呈现的Guid不是有效的:它的第一个块应该只包含4个字节,但您提供的示例需要5个字节来存储1eb4617fd 。这就是为什么我在我的例子中使用了两个字符串变量。



希望这会有所帮助。


试试这个

 受保护  void  Page_Load(对象发​​件人,EventArgs e)
{

bool a = IsHavingGUID( text1eb4617fd-4876-4c52-aa5b-21f8be8fb0f8.txt);
bool b = IsHavingGUID( text1。 TXT);
}

public bool IsHavingGUID( string input)
{
MatchCollection guids = Regex.Matches(input, @ (\ {){0,1} [0-9A-FA-F] {8} \- [0-9A-FA-F] {4} \- [0-9A -Fa-F] {4} \- [0-9A-FA-F] {4} \- [0-9A-FA-F] {12}(\}){0,1}< /跨度>);
返回 guids.Count> 0;
}


File name is text1eb4617fd-4876-4c52-aa5b-21f8be8fb0f8.txt
it should return true because it contains GUID

If File name is text1eb4617fd-4876-f8.txt or text1.txt
it should return false as it does not contain any GUID

解决方案

A regular expression would be perfect for that:

using System.Text;

string pattern = @"^text(?<guid>[0-9a-zA-Z]{8}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{12})\.txt


"; Regex regex = new Regex(pattern, RegexOptions.Compile | RegexOptions.CultureInvariant); string input1 = "text1eb4617fd-4876-4c52-aa5b-21f8be8fb0f8.txt"; string input2 = "texteb4617fd-4876-4c52-aa5b-21f8be8fb0f8.txt"; bool input1Matches = regex.IsMatch(input1); // input1Matches will be false bool input2Matches = regex.IsMatch(input2); // input2Matches will be true


As a bonus, if you want to get the Guid, it is a simple as:

Match m = regex.Match(input2);
Guid guid = Guid.Parse(m.Groups["Guid"].Value);


Note: the Guid you are presenting is not a valid one: its first block should hold exactly 4 bytes, buty the example you give would need 5 bytes to store 1eb4617fd. That's why I used two string variables in my example.

Hope this helps.


Try This one

protected void Page_Load(object sender, EventArgs e)
   {

       bool a = IsHavingGUID("text1eb4617fd-4876-4c52-aa5b-21f8be8fb0f8.txt");
       bool b = IsHavingGUID("text1.txt");
   }

   public bool IsHavingGUID(string input)
   {
       MatchCollection guids = Regex.Matches(input, @"(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}");
      return guids.Count>0;
   }


这篇关于如何检查文件名是否包含GUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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