跳過到頁腳內容
EXCEL 工具

.NET Regex 測試器測試您的正則表達式模式

在不斷發展的軟體開發領域,能夠促進高效編碼實踐的強大工具是必不可少的。 其中,正規表示式(regex)在字串操作和模式匹配中至關重要。 在 .NET 框架中,開發人員可以利用 .NET Regex Tester 的強大功能來簡化正規表示式模式的建立和測試流程。

在本文中,我們將踏上探索 .NET 正規表示式測試器提供的功能和實用工具的旅程。 正規表示式是一種簡潔而強大的語言,用於匹配字串中的模式,它已無縫整合到 .NET 框架中。 .NET 正規表示式測試器為開發人員提供了一個專門的環境,用於微調他們的正規表示式模式,並針對各種輸入場景進行測試。 該工具在調試和改進正規表示式方面發揮了不可估量的作用,最終可以編寫出更有效率、更不容易出錯的程式碼。

本文深入探討了.NET Regex Tester的功能,提供了有關其用法以及與IronXL (一個用於在 .NET 應用程式中處理 Excel 文件的功能強大的庫)集成的見解和示例。

1. .NET 正規表示式測試器簡介

.NET Regex Tester 是一個強大的基於 Web 的平台,旨在簡化和提昇在 .NET 框架中使用正規表示式的複雜性。 這款功能強大的工具為開發者提供了一個極其人性化的介面。 它提供了一個無縫環境,可以輸入複雜的正規表示式模式,針對各種樣本字串進行嚴格測試,並直觀地視覺化相應的匹配結果。 此測試工具專為 .NET 正規表示式量身定制,保證與 .NET 框架中嵌入的正規表示式引擎完美相容,從而確保模式匹配的精確性和準確性。

.NET Regex Tester 的獨特之處在於其一系列功能,包括即時匹配功能和全面的匹配訊息,這些功能共同促進了整個正則表達式開發工作流程的顯著改進。 從本質上講,該工具成為了開發人員在複雜的正規表示式領域中不可或缺的助手,提高了開發過程的效率、準確性和便利性。

2. 程式碼範例 - 使用 .NET 正規表示式測試器

為了說明 .NET 正規表示式測試器的實際應用,讓我們考慮這樣一個場景:我們需要從給定的文字中提取電子郵件地址。 以下是一個 C# 程式碼範例片段,示範如何使用 .NET 正規表示式測試器來實作此功能:

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        // Sample input text containing email addresses
        string inputText = "Sample text with email addresses: user1@example.com, user2@example.net";

        // Regex pattern to match email addresses
        string pattern = @"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b";

        // Create a Regex object with the pattern
        Regex regex = new Regex(pattern);

        // Find matches of the pattern in the input text
        MatchCollection matches = regex.Matches(inputText);

        foreach (Match match in matches)
        {
            // Output each found email address
            Console.WriteLine($"Found email: {match.Value}");
        }
    }
}
using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        // Sample input text containing email addresses
        string inputText = "Sample text with email addresses: user1@example.com, user2@example.net";

        // Regex pattern to match email addresses
        string pattern = @"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b";

        // Create a Regex object with the pattern
        Regex regex = new Regex(pattern);

        // Find matches of the pattern in the input text
        MatchCollection matches = regex.Matches(inputText);

        foreach (Match match in matches)
        {
            // Output each found email address
            Console.WriteLine($"Found email: {match.Value}");
        }
    }
}
Imports System
Imports System.Text.RegularExpressions

Friend Class Program
	Shared Sub Main()
		' Sample input text containing email addresses
		Dim inputText As String = "Sample text with email addresses: user1@example.com, user2@example.net"

		' Regex pattern to match email addresses
		Dim pattern As String = "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b"

		' Create a Regex object with the pattern
		Dim regex As New Regex(pattern)

		' Find matches of the pattern in the input text
		Dim matches As MatchCollection = regex.Matches(inputText)

		For Each match As Match In matches
			' Output each found email address
			Console.WriteLine($"Found email: {match.Value}")
		Next match
	End Sub
End Class
$vbLabelText   $csharpLabel

在這個範例中,正規表示式模式為\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[AZ]|az]{2,}\b` 用來符合輸入文字中的電子郵件地址。 .NET 正規表示式測試器可讓開發人員以互動方式試驗此類模式,使正規表示式開發流程更加直覺。

2.1 輸出影像

! .NET 正規表示式測試器(開發者使用方法):圖 1 - 上述程式碼的控制台輸出

3. IronXL簡介

IronXL是一個功能強大且用途廣泛的 .NET 程式庫,旨在簡化應用程式中 Excel 檔案的處理。 無論您是在桌面、網頁還是行動應用程式上工作,IronXL 都提供了一套強大的工具和功能,以簡化讀取、寫入和操作 Excel 檔案的過程。

IronXL 的開發充分考慮了 .NET 框架,可無縫整合到您的 C# 或 VB.NET 專案中,為 Excel 相關任務提供簡單且高效的解決方案。 無論您是建立報表、匯入資料還是執行複雜的計算,IronXL 都為開發人員提供了一套全面的 API 和方法,讓 Excel 檔案操作變得輕而易舉。

3.1. 安裝 IronXL

若要輕鬆安裝 IronXL,請使用 Visual Studio 中的 NuGet 套件管理器。 要安裝的特定軟體包名為 IronXL.Excel。 將以下指令貼到軟體套件管理器控制台中,然後按回車鍵。

Install-Package IronXL.Excel

3.1 程式碼範例 - 將 IronXL 與 .NET 正規表示式測試器集成

為了展示 .NET Regex Tester 和 IronXL 之間的協同作用,假設您想要根據特定模式從 Excel 檔案中提取資料。 以下 C# 程式碼片段示範如何將 IronXL 與 .NET 正規表示式測試器結合使用:

using IronXL;
using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        // Regex pattern to match email addresses
        string pattern = @"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b";

        // Load Excel file using IronXL
        WorkBook workbook = WorkBook.Load("datatable.xlsx");
        WorkSheet workSheet = workbook.WorkSheets[0];

        // Iterate through the specified range of cells and find matches
        foreach (var cell in workSheet["A2:A10"])
        {
            string cellValue = cell.Text;

            // Use regex to find matches within the cell text
            MatchCollection matches = Regex.Matches(cellValue, pattern);
            foreach (Match match in matches)
            {
                // Output each found match with its cell address
                Console.WriteLine($"Found match in Excel at {cell.AddressString}: {match.Value}");
            }
        }
    }
}
using IronXL;
using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        // Regex pattern to match email addresses
        string pattern = @"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b";

        // Load Excel file using IronXL
        WorkBook workbook = WorkBook.Load("datatable.xlsx");
        WorkSheet workSheet = workbook.WorkSheets[0];

        // Iterate through the specified range of cells and find matches
        foreach (var cell in workSheet["A2:A10"])
        {
            string cellValue = cell.Text;

            // Use regex to find matches within the cell text
            MatchCollection matches = Regex.Matches(cellValue, pattern);
            foreach (Match match in matches)
            {
                // Output each found match with its cell address
                Console.WriteLine($"Found match in Excel at {cell.AddressString}: {match.Value}");
            }
        }
    }
}
Imports IronXL
Imports System
Imports System.Text.RegularExpressions

Friend Class Program
	Shared Sub Main()
		' Regex pattern to match email addresses
		Dim pattern As String = "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b"

		' Load Excel file using IronXL
		Dim workbook As WorkBook = WorkBook.Load("datatable.xlsx")
		Dim workSheet As WorkSheet = workbook.WorkSheets(0)

		' Iterate through the specified range of cells and find matches
		For Each cell In workSheet("A2:A10")
			Dim cellValue As String = cell.Text

			' Use regex to find matches within the cell text
			Dim matches As MatchCollection = Regex.Matches(cellValue, pattern)
			For Each match As Match In matches
				' Output each found match with its cell address
				Console.WriteLine($"Found match in Excel at {cell.AddressString}: {match.Value}")
			Next match
		Next cell
	End Sub
End Class
$vbLabelText   $csharpLabel

這段 C# 程式碼利用 IronXL 函式庫從 Excel 檔案("datatable.xlsx")讀取資料。 它定義了一個用於匹配電子郵件地址的正規表示式模式。 然後,程式碼載入 Excel 文件,遍歷特定範圍的儲存格(第一個工作表中的 A2 到 A10),從每個儲存格中提取文本,並應用定義的正規表示式模式來尋找和列印電子郵件地址。 對於每個匹配項,代碼會輸出手機地址和匹配的電子郵件值。 該程式旨在示範如何使用 IronXL 處理 Excel 數據,並對指定範圍內的儲存格值執行正規表示式匹配。

輸入影像

.NET 正規表示式測試器(開發人員使用方法):圖 2 - 輸入的 Excel 文件

輸出圖片

! .NET 正規表示式測試器(開發者使用方法):圖 3 - 上述程式碼的控制台輸出

4.結論

總之,.NET Regex Tester 對於在 .NET 框架中使用正規表示式的開發人員來說是一個非常寶貴的工具。 其使用者友善的介面和即時匹配功能提高了正規表示式模式開發的效率。 此外,當與IronXL整合時,開發人員可以無縫擴展其功能以處理 Excel 文件,從而為資料處理和分析開闢新的可能性。 透過結合這些工具的優勢,開發人員可以創建功能強大的應用程序,並增強正規表示式和 Excel 處理能力。

IronXL 為所有用戶提供免費試用許可證,非常適合測試和開發用途。 若要取得使用 IronXL 讀取 Excel 檔案的詳細教程,請造訪此處。 這是從 NuGet 套件管理器網站下載 IronXL 的連結

Jordi Bardia
軟體工程師
Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担产品测测试,产品开发和研究的责任时,Jordi 为持续的产品改进增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。