정규 표현 식 을 사용 하여 문자열 의 여러 URL 을 대체 합 니 다.

3851 단어 정규 표현 식
 1         Regex urlregex = new Regex(@"(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?",     //                    
 2                                 RegexOptions.IgnoreCase | RegexOptions.Compiled);
 3 
 4 
 5                                 string oldProName = modelProName;
 6                                 string newProName = modelProName;
 7 
 8                                 string strtitle = modelProName;
 9 
10                                 if (urlregex.IsMatch(modelProName))    
11                                 {
12                                     foreach (Match match in urlregex.Matches(modelProName)) //Match  
13                                     {
14                                         string url = match.Value;
15                                         if (url != match.NextMatch().Value)     //           
16                                         {
17                                             //  newProName = urlregex.Replace(oldProName, "<a href=\"" + url + "\" target=\"_blank\" style=\"color:blue\">    </a>");
18                                             newProName = newProName.Replace(url, "<a href=\"" + url + "\" target=\"_blank\" style=\"color:blue\">    </a>");   //  String.Replace(),            replacement
19                                         }
20                                     }
21 
22 
23                                 }

좋은 웹페이지 즐겨찾기