Okay, so I am new to regex, or at least to actually writing them, but here is what I have:
The string:
LDAP://CN=Doe\, John,OU=Users,DC=my,DC=domain
The regex (that is not working as expected):
(?:LDAP://CN=)([a-zA-Z]+\\?[,\s]?\s?[a-zA-Z]+)
Groups matched:
LDAP://CN=Doe\, Joe
Doe\, John
Captured group:
LDAP://CN=Doe\, John
What I want to return:
Doe, John
By my understanding (which is obviously not correct) I was under the impression that if I included ?:
for a captured group it would not return it in the match; and likewise, I do not want to return \
before the ,
in the middle of the name – which I actually do not know how to exclude a character in a returned result as such. Anyone able to shine some light on the matter?
[update]
I was able to get the results being doing the following (I'm using powershell btw):
$qryResult = "LDAP://CN=Doe\, John,OU=Users,DC=my,DC=domain"
[regex]$re = "LDAP://CN=(.*?),OU"
$result = $re.Match($qryResult)
(($result.Value -replace "LDAP://CN=","") -replace "\\","") -replace ",OU",""
But it would nice to use regex from start to finish replacing the text like so. It's possible?
Change your PowerShell to this:
It will target only the second group (your capture group):
.Groups[1]