-1

I want to find User Principal names inside all sort of strings for a C# tool to anonymize customer specific data.

I found this one:

(?[^@]+)@(?(?:[a-z0-9]+.)+[a-z]+)

But in a string like this:

f-4af0-86e8-01439a0ae52a\",,\"Active\",\"10/30/2018 9:05:35 AM\",\"SingleSession\",\"Desktop\",,\"10/29/2018 2:35:06 PM\",,\"655952\",\"DOM\na010318\",\"na010318\",\"DOM\n010318\",\"S-1-5-21-2052699199-3915784498-1582209984-1157056\",\"user.a@domain.acc.local\",\"Primary\",\"c46b084c-6df3-47dd-9d3e-8e17f855c7fe\""

It matches the entire part before the UPN (the first space, because it matches the word I guess.

How can I re-write the regular expression to only find the e-mail/UPN within this string?

Thanks

8biT
  • 135
  • 1
  • 8
Rahvin47
  • 91
  • 5
  • regex for RFC822 email address: http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html – Toto Dec 18 '18 at 13:09
  • 1
    It seems to me that it's a csv file, why don't you use a csv parser? – Toto Dec 18 '18 at 13:18
  • Try `[\w\.]+@[\w\.]+` but it will break for cases that are more complex. – marsze Dec 18 '18 at 13:19
  • The simplest way: `[^"@]+@[^"]+` – Toto Dec 18 '18 at 13:29
  • Cannot use a csv parser because I'm collecting from a multitude of sources, so I read all lines as text and try to filter out any customer specific info. Thanks though! – Rahvin47 Dec 18 '18 at 13:36
  • Reading all the text on huge files will use lots of memory and make you code more complicated than necessary. Read one line at a time is more efficient and much less complicated. – jdweng Dec 18 '18 at 13:42

1 Answers1

0

If a UPN effectively has the same format as an email address - try some of the answers from this question:

regex extract email from strings

The following regex works for your example:

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

https://regex101.com/r/ijG8Xr/2

Stradlater
  • 56
  • 2
  • That last one seems to work like it should. Do you also know how to filter out things like domain\username ? I'm very afraid for false positives there... – Rahvin47 Dec 18 '18 at 13:34