출처: http://www.codeproject.com/aspnet/AutoHyperlinks.asp
{
// (c)2006 Michael Argentini, http://www.nonsequiturs.com.
string final = strvar;
Regex regex = new Regex(@"<nolink>(.*?)</nolink>",
RegexOptions.IgnoreCase | RegexOptions.Singleline |
RegexOptions.CultureInvariant |
RegexOptions.IgnorePatternWhitespace |
RegexOptions.Compiled);
MatchCollection theMatches = regex.Matches(strvar);
for (int index = 0; index < theMatches.Count; index++)
{
final = final.Replace(theMatches[index].ToString(),
theMatches[index].ToString().Replace(".", "[[[pk:period]]]"));
}
regex = new Regex(@"<a(.*?)</a>", RegexOptions.IgnoreCase |
RegexOptions.Singleline | RegexOptions.CultureInvariant |
RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled);
theMatches = regex.Matches(final);
for (int index = 0; index < theMatches.Count; index++)
{
final = final.Replace(theMatches[index].ToString(),
theMatches[index].ToString().Replace(".", "[[[pk:period]]]"));
}
final = Regex.Replace(final, @"(?<=\d)\.(?=\d)", "[[[pk:period]]]");
Regex tags = new Regex(@"([a-zA-Z0-9\:/\-]*[a-zA-Z0-9\-_]\" +
@".[a-zA-Z0-9\-_][a-zA-Z0-9\-_][a-zA-Z0-9\?\" +
@"=&#_\-/\.]*[^<>,;\.\s\)\(\]\[\""])");
final = tags.Replace(final, "<a href=\"http://$&\"" +
param + ">$&</a>");
final = final.Replace("http://https://", "https://");
final = final.Replace("http://http://", "http://");
final = final.Replace("http://ftp://", "ftp://");
final = final.Replace("http://rtsp://", "rtsp://");
final = final.Replace("http://mms://", "mms://");
final = final.Replace("http://pcast://", "pcast://");
final = final.Replace("http://sftp://", "sftp://");
final = final.Replace("[[[pk:period]]]", ".");
final = final.Replace("<nolink>", "");
final = final.Replace("</nolink>", "");
}
string strvar 는 변환이 필요한 값이고,
string param 는 실제 하이퍼링크 태그내에서의 속성값, 즉 target="_blank" 같은거나,. class="aaa", style="bbb" 입니다.
댓글 없음:
댓글 쓰기