views:

31

answers:

3

I have a nomenclature to respect while performing some tasks against the Active Directory.

Here's the nomenclature:

  • TT-EEE-Mnemonic: if TT = 'GA' or 'GS' or 'PA' or 'PF' -> the schema to create is a "group", with a groupScope of Global.
    1. LT-EEE-Mnemonic: if T = 'A' or 'G' or 'I' or 'N' or 'P' -> the schema to create is a "group", with a groupScope of Domain local.
    2. TTT-EEE-Mnemonic: if TTT* = 'CNX' or 'GST' or 'SVC' -> the shema to create is an "user"
    3. T-SSSS-Mnemonic: if T = 'A' or 'L' or 'M' or 'R' or 'S' -> the schema to create is an "organizationUnit"

What I'm after is a simpler and more effective way than this:

If(dn.Substring(3, 2).Contains("GA") _
    Or variable.Substring(3, 2).Contains("GS") _
    Or dn.Substring(3, 2).Contains("PA") _
    Or dn.Substring(3, 2).Contains("PF")) Then 
    schema = "group" ' Global'
Else If(dn.Substring(4, 1).Contains("A") _
    Or dn.Substring(4, 1).Contains("G") _
    Or dn.Substring(4, 1).Contains("I") _
    Or dn.Substring(4, 1).Contains("N") _
    Or dn.Substring(4, 1).Contains("P")) Then
    schema = "group" ' Local'
Else If(dn.Substring(3, 3).Contains("CNX") _
    ' Well... You get the idea, don't you?
End If

I guess I could use a RegularExpression, or perhaps one for each of the nomenclature I got, something alike.

Is there a way a RegularExpression could become handy in this situation? Or would it be best to stick with that old big-If? Any suggestions are welcome.

Sorry for asking, but I'm not used to use RegularExpression. I know they exist, and bit of what they can do, but that's all.

Thanks!

+1  A: 

How about...

Dim Schema As String = Nothing
Select Case dn.SubString(3, 2) ' Am not sure about your index of 3 here!
  Case "GA", "GS", "PA", "PS"
    Schema = "group"
End Select

If IsNothing(Schema) Then
  Select Case ...
End If

etc.
Will A
...admittedly not regex but then why bother when a simple solution exists?
Will A
The index of 3 is because I got the distinguishedName value of the object to create into the underlying directory. Its form is: "CN=TT-EEE-Mnemonic,OU=lowerOU,OU=middleOU,OU=highestOU,DC=my,DC=domain,DC=com". In order to get the `TT`, I need to access index 3. =)
Will Marcouiller
Even though not a `RegEx`, this is a simple solution, and simpler for a colleague who who have to debug my code. I agree, I don't bother much if the best solution isn't `RegEx`, I just thought it would be simpler with it. =)
Will Marcouiller
+2  A: 

Your code does not seem to conform your description. With your description, you may want the following regular expression:

^(((GA|GS|PA|PF)|L[AGINP]|(CNX|GST|SVC))-EEE|[ALRMS]-SSSS)$

EDIT: you may want to read up this tutorial about what the regular expression means, specifically look for the "Character classes" and "Grouping and alternatives" sections.

In short, the bar character (i.e. |) is the "OR" operator. The square brackets (i.e. []) are the character class; in other words, "OR" between the characters.

William
How many Will(iam)'s involved in one post - scary!
Will A
LOL! (too much characters are required here...)
Will Marcouiller
@William: I may recognize the characters I'm after, and even come to a little understanding of the `RegEx` suggested. Might you explain it just a little bit more so that I don't need to come and bother SO because I can't write one alone as I'm not used to it? Thanks for your help! =)
Will Marcouiller
From this, how can I differentiate whether it's a "user", an "organizationalUnit", a "group" (local) or a "group" (global) clearly, so that I may pass the obtained schema based on the `RegEx` to pass to my Create() method? Thanks for your time.
Will Marcouiller
lol~~~~~~~~~ :D
William
@William: Thanks for your explanation, and double thanks for the tutorial link. =)
Will Marcouiller
+1  A: 

It'd vastly reduce the number of tests and explicit Ors.

If Regex.IsMatch(dn, "^CN=(G[AS]|P[AF])-") Then
    schema = "group"    ' Global                 'damn syntax highlighting
ElseIf Regex.IsMatch(dn, "^CN=L[AGINP]-") Then
    schema = "group"    ' Local                  'damn syntax highlighting
ElseIf Regex.IsMatch(dn, "^CN=(CNX|GST|SVC)-") Then
    schema = "user"
ElseIf Regex.IsMatch(dn, "^CN=[ALMRS]-") Then
    schema = "organizationUnit"
End If
cHao
@cHao: Thanks! That seems to reflect the idea behind my question.
Will Marcouiller
@cHao: What the ^ character means in RegEx?
Will Marcouiller
@Will Marcouiller: It anchors the pattern to the start of the string. Without it, the pattern will match (for example) "CN=GA-" anywhere in the string -- which might not be a bad thing, if for some reason you can't guarantee that what you're looking for is at the start.
cHao
In LDAP, the *distinguishedName* property always begins with the name of the object (CN), and from bottom-up with the OU's until the end of the domain name. I get your point though, but in my situation, I absolutely need it in the beginning of the string.
Will Marcouiller
Then the ^ is appropriate. :)
cHao
If I understand correctly, the ^ character obliges the RegEx to match from the beginning of the string. The characters you want constant, you simply put them in. You put within square brackets the values that a certain position can have, and you separate each combination with a | character. What about the parentheses? (Am I right in my affirmation, I'm trying to learn meanwhile. =))
Will Marcouiller
The square brackets will match any one of the characters inside them. `[ALMRS]`, for example, matches either A, L, M, R, or S. The `|` is pretty much like an "or", but it's rather odd prioritywise -- without parentheses, it can cut a regex right in two. If the parentheses weren't there, it'd the first pattern would match "CN=GA some garbage" or "some garbage PF-more garbage" etc, because the ^ is part of the first alternative, not the whole expression. You need to use some kind of grouping to fix that.
cHao
Parentheses can also tell a regex to capture the part that'd be inside them, and return it as a string. But if you're just looking for matches, they don't do much except group stuff.
cHao