Regex - Parsing two strings (first and last name) within string of text

Viewed 65

Here is what I have which is likely a stroke of luck because this is probably my first week of messing around with regex:

((?<lastname>(?![CN=]))(?<firstname>\w*))

Here is an example string:

CN=Doe\, John,OU=Windows10WS,OU=Employees,OU=Users,OU=AFR,DC=org,DC=bssz,DC=com
CN=Doo\, Scooby,OU=WindowsSRV,OU=Manager,OU=Users,OU=NYC,DC=org,DC=nyc,DC=com

My regex definitely matches all words, but I only need to keep the first two matches ((Doe John) (Doo Scooby)

There is only ever one line of text. The names are always in the same positions (lastname :after CN= until ) (firstname : next word after [space] until ,)

Any help would be great, and any material to look up why my regex isn't appropriate but why the suggested solution is would be greatly beneficial! I'm trying to get more useful information into our SIEM.

1 Answers

You can use

^CN=(\w+)\W+(\w+).*

and replace with $1 $2.

See the regex demo.

Regex details:

  • ^ - string start
  • CN= - CN= text
  • (\w+) - Group 1 ($1 refers to the group value): one or more word chars
  • \W+ - one or more non-word chars
  • (\w+) - Group 2 ($2 refers to the group value): one or more word chars
  • .* - any zero or more chars other than line break chars, as many as possible.
Related