VerbalExpressions 口語式的表達式驗證

  • 2193
  • 0

VerbalExpressions 提供口語式的表達式驗證,我們可以在 NuGet 輸入關鍵字 VerbalExpressions 取得套件。

 

簡介

VerbalExpressions 提供口語式的表達式驗證,我們可以在 NuGet 輸入關鍵字 VerbalExpressions 取得套件。

SNAGHTMLd8c1f1c

 

安裝後,添加了 VerbalExpressions.dll。

image

 

image

 

測試範例

驗證字串,字串規則是一開始要有 http,也許有 s,然後是 :www.dotblogs.com,也許有 .tw,也許有 /,接著有任意字串但是不能有空白

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplicationVerbalExpressions
{
    class Program
    {
        static void Main(string[] args)
        {
            var verbEx = new VerbalExpressions.VerbalExpression()
                                            .StartOfLine()
                                            .Then("http")
                                            .Maybe("s")
                                            .Then("://www.dotblogs.com")
                                            .Maybe(".tw")
                                            .Maybe("/")
                                            .AnythingBut(" ")
                                            .EndOfLine();

            var testStrArr = new string[] { 
                "http://www.dotblogs.com.tw", 
                "http://www.dotblogs.com.tw/", 
                "https://www.dotblogs.com.tw/chou",
                "http://www.cnblogs.com"
            };

            foreach(var testStr in testStrArr)
            {
                Console.WriteLine(verbEx.Test(testStr));
            }
            Console.ReadKey();
        }
    }
}

測試結果,前三個字串符合規則,最後一個字串不符合規則。

image

 

其他相關資訊

VerbalExpressions