2014-06-27 42 views
0

幫助如何檢測一個鏈接收藏格式

我想在新的窗口 ,bookmarkformt打開URL網址在當前的Webbrowser開放

我有一個簡單的方法來檢測書籤格式的URL

文件:/// d:/Administrator/Desktop/123.htm#222

含有的.htm#或.htm#

這是正確的書籤格式的URL

//書籤名稱是222

文件:/// d:/Administrator/Desktop/123.htm#222

//書籤名稱是## ABC

文件:/// d:/Administrator/Desktop/123.htm###abc

//書籤名稱//.HTM##.htm#abc#.html# #//

文件:/// d:/Administrator/Desktop/123.htm#//.HTM##.htm#abc#.html##//

如何檢測一個鏈接收藏格式? RegularExpressions可以解決這個問題,我不能寫一個corrent正則表達式?

這是我的解決方案,但需要dectect URL是書籤格式

string htmFilename = @"D:\Administrator\Desktop\123.htm"; 
private void Form1_Load(object sender, EventArgs e) 
{ 
    webBrowser1.StatusTextChanged += new EventHandler(webBrowser1_StatusTextChanged); 
    webBrowser1.Navigate(htmFilename); 
    navigated = true; 
} 


private void webBrowser1_StatusTextChanged(object sender, EventArgs e) 
{ 
    textBox1.Text = webBrowser1.StatusText; 
} 

//webBrowser1 Navigating the first time not open in new window 
bool navigated = false; 
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e) 
{ 
    //first time nagivate 
    if (navigated == true) 
    { 
     // url is not bookmark format 
     // open in new window 
     if (e.Url.ToString().Contains(".htm#") == false) 
     { 
      e.Cancel = true; 
      //webBrowser1.Navigate(e.Url, true); 

      //Process.Start is bettor then webBrowser1.Navigate(e.Url, true); 
      //Because when url is directory link such as "file:///C:/" 
      //webbrowser will open a blank window and then open the Directory 

      System.Diagnostics.Process.Start(e.Url.ToString()); 
     } 
    } 
    textBox2.Text = e.Url.ToString(); 
} 

回答

0

下面的正則表達式將匹配正確的書籤格式,

file:\/\/\/[^\.]*\.(?:htm|html)#+.* 

DEMO

OR

file:\/\/\/D:\/Administrator\/Desktop\/123\.htm#.* 

DEMO