English 中文(简体)
在使平原案文中可以点击的雷尔语方面
原标题:Regex string issue in making plain text urls clickable

在C#中,我需要一部能探测透镜(http/https/ftp/ftps)的操作性雷达码,并用同样的圆顶把 anchor放在一起点击。 我已经采取了雷克斯模式,该守则附后如下。

然而,如果在座标中已经存在任何可点击的圆顶,那么上述代码就把另一个 anchor子放在了上面。 例如,在以下法典中的现有次数:“ftp://www.abc.com >ftp://www.abc.com,在以下代码操作时,该代码有另一个封顶。 是否有办法加以确定?

        string sContent = "ttt <a href= ftp://www.abc.com >ftp://www.abc.com</a> abc ftp://www.abc.com abbbbb http://www.abc2.com";

        Regex regx = new Regex("(http|https|ftp|ftps)://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&amp;\*\(\)_\-\=\+\\\/\?\.\:\;\ \,]*)?", RegexOptions.IgnoreCase);

        MatchCollection mactches = regx.Matches(sContent);

        foreach (Match match in mactches)
        {
            sContent = sContent.Replace(match.Value, "<a href= " + match.Value + " >" + match.Value + "</a>");
        }

另外,我希望一部雷克斯法典将电子邮件作为可点击的“邮件”标签。 我可以自己做,但上述双管束问题也将出现在其中。

最佳回答

我在你的示范测试中注意到,如果像ftp://www.abc.comsContent中的对应内容。

下面的法典例子应当给你想要的东西:

string sContent = "ttt <a href= ftp://www.abc.com >ftp://www.abc.com</a> abc ftp://www.abc.com abbbbb http://www.abc2.com";

Regex regx = new Regex("(?<!(?:href= |<a[^>]*>))(http|https|ftp|ftps)://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&amp;\*\(\)_\-\=\+\\\/\?\.\:\;\ \,]*)?", RegexOptions.IgnoreCase);

MatchCollection matches = regx.Matches(sContent);

for (int i = matches.Count - 1; i >= 0 ; i--)
{
    string newURL = "<a href= " + matches[i].Value + " >" + matches[i].Value + "</a>";

   sContent = sContent.Remove(matches[i].Index, matches[i].Length).Insert(matches[i].Index, newURL);
}
问题回答

我知道我来得很晚才来到这个政党,但是,在议会中有一些问题,即现有的答案没有解决。 首先,最令人厌恶的是,有这种树林。 如果你使用C#的逐字记录,那么你就不必做这样重复的事情。 无论如何,大多数的反弹首先是需要的。

其次,这个比值:([/w+?/w+]+。 方括号构成一个特性类别,其内的一切都作为字面特性处理,或作为“条码”一类的短体处理。 但是,删除方括号是不够的。 我怀疑你试图做的是:w+(?:w+)+

第三,在reg末端的qu-* ? - 是不匹配的。 ,详情请上

There are other, minor problems, but I won t go into them right now. Here s the new and improved regex:

@"(?n)(https?|ftps?)://w+(.w+)+([-a-zA-Z0-9~!@#$%^&*()_=+/?.:; ,\]*)(?![^<>]*+(>|</a>))"

The negative lookahead - (?![^<>]*+(>|</a>)) is what prevents matches inside tags or in the content of an anchor element. It s still very crude, though. There are several areas, like inside <script> elements, where you don t want it to match but it does. But trying to cover all the possibilities would result in a mile-long regex.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签