Ryan Pearce
Ryan Pearce

Reputation: 23

My regular expression is still capturing a non-captured group

I have the following regular expression:

(?:^(?:X-Rcpt-To|X-MDRcpt-To|To): (?:[\<\'\"])?(.*?)(?:[\>\'\"])?$)

And the following block of text:

To: <[email protected]>

It captures the email that it's supposed to here: https://regex101.com/r/gQ1bV5/1

But when I implement this within my code, it still captures > etc at the end of the email address. (Returning [email protected]>)

public string FindRealEmail()
{
    var filterRegex = new Regex("(?:^(?:X-Rcpt-To|X-MDRcpt-To|To): (?:[\\<\\\'\\\"])?(.*?)(?:[\\>\\\'\\\"])?$)", RegexOptions.Multiline);
    var email = (filterRegex.IsMatch(Body) ? filterRegex.Match(Body).Groups[1].Value : EmailAddress).Replace("\r", "");
    return email;
}

Upvotes: 2

Views: 53

Answers (1)

Howwie
Howwie

Reputation: 275

The issue is not really because of the last non-capturing group but the line endings. If you handle the line feed at the end, the > isn't captured.

(?:^(?:X-Rcpt-To|X-MDRcpt-To|To): (?:[\<\'\"])?(.*?)(?:[\>\'\"])?\r?$)

If you don't need to capture certain sections, you can just omit the parentheses. You don't have to escape the <, ' & " characters either.

^(?:X-Rcpt-To|X-MDRcpt-To|To): [<'"]?(.*?)[>'"]?\r?$

Upvotes: 3

Related Questions