RegEx电子邮件无法正常工作

时间:2015-02-15 20:09:41

标签: c# regex

外观和声音非常简单 - 在代码的其他部分工作的正则表达式很好,这里它不工作 - 总是返回" N / A"。无法解决这个问题?

private string FindEmail(string description)
        {
            try
            {
                const string pattern = @"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$";
                Match m = Regex.Match(description, pattern);
                string emailaddr = m.Success ? m.Value : "N/A";
                return emailaddr;
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Error in Link Data Repository {0} in Parse Links {1}", ex.Message, ex.StackTrace);
                throw new Exception(ex.Message);
            }
        }

描述字符串看起来像这样:

Phase 1 plots in Al Furjan
75% paid
25% to pay in November 2014

6,544 sq ft plot with BUA of 4,908 sq ft 

Call Nicoleta 055-5573564 or email: nicoleta.mihoc@group7properties.com

Al Furjan is located in a convenient location just off the Emirates Road and the Dubai Investment Park Road within the Jebel Ali community Zone. This 560 Hectare Mega development is consisted of family town houses & villas of 3 to 6 bedrooms, and will offer a much needed boost for family villas within the commuter zone of Dubai.

Inspired by a historic Arabic phrase from Dubai's proud past, Al Furjan symbolizes a collection of homes or a small village. 
A "fareej" (a single village) represented a way of life to its residents, one that created a community of extended family and friends, rather than merely neighbors.

2 个答案:

答案 0 :(得分:1)

由于电子邮件地址被其他文本包围,因此您需要在正则表达式模式之前和之后允许文本。这可以通过以下方式实现:

string pattern = @"[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+";

因此,不同之处在于此模式不包含^$

答案 1 :(得分:0)

您需要从正则表达式中删除锚点,因为电子邮件地址不是一行中唯一存在的地址。 Anchor ^断言我们在一行的开头,$断言我们在一行的末尾。

@"[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+"

DEMO