如何检测Url是BookMark格式

时间:2014-06-27 05:30:28

标签: regex url bookmarks

帮助

我想在新窗口中打开网址 ,bookmarkformt url在当前的Webbrowser中打开

我有一种简单的方法来检测书签格式网址

  

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

包含.htm#或.htm#

这是正确的书签格式网址

  

//书签名称为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##//

如何检测网址是BookMark格式? RegularExpressions可以解决这个问题,我不能写一个正确的正则表达式吗?

这是我的解决方案,但需要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();
}

1 个答案:

答案 0 :(得分:0)

以下正则表达式将匹配正确的书签格式

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

DEMO

OR

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

DEMO