Trying to get .NET regex to recognize the following string (A), but not string (B). Specific requirements to use .NET flavor of regex.
(A) :: John Doe <JDoe@domain.com>
(B) :: John Doe <JDoe@domain.com>, Jane Dina <JDina@domain.com>
Have tried quite a few options here including the following, but haven't gotten it to work the way I need it to (for more context, this is going to be used in an Exchange Online Transport Rule to filter messages that have ONLY one recipient, no more than one.
^[^<]+<[^>]+>$ - fails, recognizes both
^.*(?=[^,])$ - fails, recognizes none
^[^,]$ - fails, recognizes both
Let me know if there are additional details I can provide.
Dustin
The following regex - the first one you mention as failing - does work, with individual single-line strings as input:
^[^<]+<[^>]+>$
With multiline input, you need the Multiline
regex option, which can be specified inline via (?m)
, so that ^
and $
match on each line:
(?m)^[^<]+<[^>]+>$
For an explanation of the regex and the option to experiment with it, see this regex101.com page.
Using PowerShell to demonstrate the solution:
# Matches and outputs only the (A) input string.
@(
'(A) :: John Doe <JDoe@domain.com>',
'(B) :: John Doe <JDoe@domain.com>, Jane Dina <JDina@domain.com>'
) -match '^[^<]+<[^>]+>$'
[regex]::Matches(
@'
(A) :: John Doe <JDoe@domain.com>
(B) :: John Doe <JDoe@domain.com>, Jane Dina <JDina@domain.com>
'@,
'(?m)^[^,]+<[^>]+>$'
).Value