0

将字符串中的所有链接和电子邮件地址与列表数组匹配的最简单方法是什么?我preg_match在 PHP 中使用,但在 C# 中看起来会有所不同。

4

2 回答 2

1

假设您已经有一个有效的正则表达式,您可以使用Regexclass,如下所示:

static readonly Regex linkFinder = new Regex(@"https?://[a-z0-9.]+/\S+|\s+@\S+\.\S+", RegexOptions.IgnoreCase);

foreach(Match match in linkFinder.Matches(someString)) {
    //Do things...
    string url = match.Value;
    int position = match.Index;
}
于 2010-06-09T14:04:43.233 回答
-1

这应该适用于链接:

https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?

来源

这应该适用于电子邮件地址:

[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}

来源

于 2010-06-09T14:08:58.523 回答