我正在构建一个在文本中搜索 url 并将 url 添加到列表框的应用程序。我有一些工作,但是如果它们结束了句子,我无法获取 URL(例如:这是 www.google.com。)。提前致谢
这是我的代码:
private void btnExtract_Click(object sender, EventArgs e)
{
StringBuilder taintedStr = new StringBuilder(txtInputText.Text);
string cleanStr;
taintedStr.Replace(",", "");
taintedStr.Replace("!", "");
taintedStr.Replace("(", "");
taintedStr.Replace(")", "");
taintedStr.Replace("[", "");
taintedStr.Replace("]", "");
taintedStr.Replace("http://", "");
cleanStr = taintedStr.ToString();
string[] wordlist = Regex.Split(cleanStr, @"\s");
for (int i = 0; i < wordlist.Length; i++)
{
bool test = Regex.Match(wordlist[i], @"^[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$").Success;
if (test == true)
{
lstWebsites.Items.Add("http://" + wordlist[i]);
}
}
}