Reputation: 23
I'm building a Active Directory "miner" in c# and would need to assert that the imput string is indeed a valid (correct "syntax") organizational unit.
an organizational unit is built up like this: CN=SomeName,OU=SomeOrganizationalUnit,DC=SomeDomainPart,DC=SomeDomainPart,DC=SomeDomainPart
It doesn't have to contain CN= or OU=, it can consist of at least two DC=, Eg:
"DC=example,DC=com"
So what I need is something that match two characters(A-z), then "=", then a word(a-zAZ0-9) then a comma, this group may occur infinite times but at least two must exist. The entire string may never end with a comma.
I'm still mostly in the copy-page state of regexp. this is what I got for starters
.[a-zA-Z0-9]{2}.[=]\W
Upvotes: 0
Views: 1494
Reputation:
You can do something like this (starting with Lloyd's original regex):
^(?:(?:CN|OU|DC)\=\w+,)*(?:CN|OU|DC)\=\w+$
Match zero or more groups followed by a comma, then one final group.
If you want to require there at least be two total groups, just change *
to +
:
^(?:(?:CN|OU|DC)\=\w+,)+(?:CN|OU|DC)\=\w+$
If you want to require that the number of groups is within a certain range, use a quantifier:
^(?:(?:CN|OU|DC)\=\w+,){1,3}(?:CN|OU|DC)\=\w+$
This would require between two and four groups total.
If you have other requirements that you want to enforce (such as always must contain DC, or has at least two DC if OU and CN are absent), then you are asking too much of a single regex. You will need a multi-step approach.
Upvotes: 1
Reputation: 50144
The following regular expression will match (two characters A-z, equals, one or more characters A-z0-9) two or more times, with a comma between each:
^[A-Za-z]{2}=[A-Za-z0-9]+(,[A-Za-z]{2}=[A-Za-z0-9]+)+$
Upvotes: 1