我有以下功能:
public static string ReturnEmailAddresses(string input)
{
string regex1 = @"\[url=";
string regex2 = @"mailto:([^\?]*)";
string regex3 = @".*?";
string regex4 = @"\[\/url\]";
Regex r = new Regex(regex1 + regex2 + regex3 + regex4, RegexOptions.IgnoreCase | RegexOptions.Multiline);
MatchCollection m = r.Matches(input);
if (m.Count > 0)
{
StringBuilder sb = new StringBuilder();
int i = 0;
foreach (var match in m)
{
if (i > 0)
sb.Append(Environment.NewLine);
string shtml = match.ToString();
var innerString = shtml.Substring(shtml.IndexOf("]") + 1, shtml.IndexOf("[/url]") - shtml.IndexOf("]") - 1);
sb.Append(innerString); //just titles
i++;
}
return sb.ToString();
}
return string.Empty;
}
如您所见,我以“markdown”格式定义了一个网址:
[url = http://sample.com]sample.com[/url]
同样,电子邮件也以这种格式编写:
[url=mailto:service@paypal.com.au]service@paypal.com.au[/url]
但是,当我传入多行电子邮件地址时,只有多个电子邮件地址,它才会返回第一封电子邮件。我希望它有多种匹配,但我似乎无法让它工作?
例如
[url=mailto:service@paypal.com.au]service@paypal.com.au[/url] /r/n a whole bunch of text here /r/n more stuff here [url=mailto:anotheremail@paypal.com.au]anotheremail@paypal.com.au[/url]
这只会返回上面的第一封电子邮件吗?
答案 0 :(得分:2)
模式的mailto:([^\?]*)
部分匹配输入字符串中的所有内容。您需要将结束括号]
添加到排除字符的内部,以限制该部分溢出“mailto”部分以外的“url”标记内的文本:
\[url=mailto:([^\?\]]*).*?\[\/url\]
请参阅此链接以获取示例:https://regex101.com/r/zcgeW8/1
答案 1 :(得分:0)
您可以借助积极的前瞻和积极的外观提取所需的结果。见http://www.rexegg.com/regex-lookarounds.html
尝试正则表达式:(?<=\[url=mailto:).*?(?=\])
以上正则表达式将从示例字符串中捕获两个电子邮件地址
[url=mailto:service@paypal.com.au]service@paypal.com.au[/url] /r/n a whole bunch of text here /r/n more stuff here [url=mailto:anotheremail@paypal.com.au]anotheremail@paypal.com.au[/url]
结果:
service@paypal.com.au
anotheremail@paypal.com.au