原文來自: YART - Yet Another Regex Tester
I know, I know, there are soooo many regular expression tester tools available on the 'Net, but I couldn't help creating my own. It's very simple, yet it supports all the basic features you'd espect from a tool of its kind, including code generation (VB and C#) and compilation to stand-alone assemblies. Best of all, it comes with source code. You can download it from the home page of my Visual Basic 2005 book (together with all other code samples in the book) or directly from here:
Executable (requires .NET Framework 2.0): RegexTester.zip (75.57 KB)
Source code (VB2005): RegexTester source.zip (88.15 KB)
Using the tool is quite simple. The main window is divided in three panes: (a) the pane where you enter the regex, (b) the pane where you load the text the regex must be applied to, (c) the result pane. A fourth pane appears when you select the Replace item from the Commands menu, and it's where you enter the replace pattern. As you can see in the image below, you can enter most regex patterns by selecting them from the context menu:
You select the kind of command (Find, replace, Split) from the Commands menu and you select one or more regex options from the Options menu:
After selecting the proper options, you press the F5 key (the Run item in the Commands menu) to execute the regex. Results are displayed in the bottom pane in a variety of formats and sort orders, which you can select from the Results menu, and the status bar displays the number of matches, the execution time, and the properties of the result currently highlighted in the result pane:
Alternatively, you can set all these options from the Properties dialog box (the Properties command in the File menu, or just press the F4 key):
Assigning a name to the current regex is important because you can save it on disk in a file with .regex extension, for later retrieval.
The Commands menu contains a couple of other interesting items. First, you can generate the C# or VB code for the current regular expression and copy it to the Clipboard:
Second, and more interesting, you can compile one or more regular expressions (including saved .regex projects) into a compiled assembly, which you can later reference from any .NET application. Using such compiled regexes is obviously faster than defining them in code, because you skip the parsing step:
That's it. You can use the YART tool for your own use, study its source code, modify and expands it as you like. If you find any major problems or add some noteworthy feature, just let me know.
如果你不覺得Regex很難讀寫的話,要麼你是一個天才,要麼,你不是地球人。Regex的文法很令人頭疼,即使對經常使用它的人來說也是如此。由於難於讀寫,容易出錯,所以找一種工具對Regex進行測試是很有必要的。
由於在不同的環境下Regex的一些細節是不相同的,本教程介紹的是微軟 .Net Framework 2.0下Regex的行為,所以,我向你介紹一個.Net下的工具Regex Tester。首先你確保已經安裝了.Net Framework 2.0,然後下載Regex Tester。這是個綠色軟體,下載完後開啟壓縮包,直接運行RegexTester.exe就可以了。
下面是Regex Tester運行時的
再介紹幾個平時收藏的關於Regex的網站,通俗易懂的:
Regex30分鐘入門教程
(在介紹中國固定電話號碼的時候有點沒有說清楚,國內有3位區號+7位號碼的和2位區號+8位號碼的)
正確的寫法應該是:^\(0\d{2}\)\d{8}$|^0\d{2}-\d{8}$|^\(0\d{3}\)\d{7}$|^0\d{3}-\d{7}$
這樣就不會出現"(021-6666888"或者"(0816-6668888"的出現了。
微軟的關於正則的說明