浏览代码

添加通配符匹配字符串方法

yuzhengyang 8 年之前
父节点
当前提交
8b953fc16b
共有 2 个文件被更改,包括 22 次插入10 次删除
  1. 1 9
      Fork.Net/Test/Y.Test/Program.cs
  2. 21 1
      Fork.Net/Y.Utils/DataUtils/StringUtils/StringTool.cs

+ 1 - 9
Fork.Net/Test/Y.Test/Program.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Text.RegularExpressions;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 using Y.Test.Commons;
@@ -20,15 +21,6 @@ namespace Y.Test
         [STAThread]
         static void Main()
         {
-            //string param = string.Format("id={0}&text={1}", "123123123", "123123123");
-            ////string rs = HttpTool.Post("http://localhost:20001/Data/Post", param);
-            //WebAPIMessageModel rs = HttpTool.Post<WebAPIMessageModel>("http://localhost:20001/Data/Post", param);
-
-            //var a = DirTool.Parent(@"D:\Temp\流量测试\");
-            //var b = DirTool.Parent(@"D:\Temp");
-            //var c = DirTool.Parent(@"D:\");
-            //var d = DirTool.Parent(@"\\\\");
-            
             Application.EnableVisualStyles();
             Application.SetCompatibleTextRenderingDefault(false);
             Application.Run(new MainForm());

+ 21 - 1
Fork.Net/Y.Utils/DataUtils/StringUtils/StringTool.cs

@@ -1,13 +1,14 @@
 //************************************************************************
 //      https://github.com/yuzhengyang
 //      author:     yuzhengyang
-//      date:       2017.3.29 - 2017.8.3
+//      date:       2017.3.29 - 2017.8.16
 //      desc:       字符串工具类
 //      Copyright (c) yuzhengyang. All rights reserved.
 //************************************************************************
 using System;
 using System.Collections.Generic;
 using System.Text;
+using System.Text.RegularExpressions;
 using Y.Utils.DataUtils.Collections;
 
 namespace Y.Utils.DataUtils.StringUtils
@@ -64,5 +65,24 @@ namespace Y.Utils.DataUtils.StringUtils
             }
             return 0;
         }
+        /// <summary>
+        /// 根据通配符验证字符串
+        /// </summary>
+        /// <param name="s">字符串</param>
+        /// <param name="pattern">通配符:%和_</param>
+        /// <returns></returns>
+        public static bool IsMatch(string s, string pattern)
+        {
+            try
+            {
+                //key = key.Replace("%", @"[\s\S]*").Replace("_", @"[\s\S]");
+                pattern = pattern.Replace("%", ".*").Replace("_", ".");
+                return Regex.IsMatch(s, pattern);
+            }
+            catch
+            {
+                return false;
+            }
+        }
     }
 }