VBS script to report AD groups - Regex pattern not working with multiple matches

424 views Asked by At

Having an issue with getting a regex statement to accept two expressions.

The "re.pattern" code here works:

If UserChoice = "" Then WScript.Quit  'Detect Cancel
re.Pattern = "[^(a-z)^(0,4,5,6,7,8,9)]"
re.Global = True    
re.IgnoreCase = True
if re.test( UserChoice ) then 
   Exit Do
 End if
 MsgBox "Please choose either 1, 2 or 3 ", 48, "Invalid Entry"

While the below "regex.pattern " code does not. I want to use it to format the results of a DSQUERY command where groups are collected, but I don't want any of the info after the ",", nor do i want the leading CN= that is normally collected when the following dsquery is run: "dsquery.exe user forestroot -samid "& strInput &" | dsget user -memberof")

The string I want to format would look something like this before formatting:

CN=APP_GROUP_123,OU=Global Groups,OU=Accounts,DC=corp,DC=contoso,DC=biz

This is the result I want:

APP_GROUP_123

Set regEx = New RegExp
**regEx.Pattern = "[,.*]["CN=]"**
Result = regEx.Replace(StrLine, "")

I'm only able to get the regex to work when used individually, either

   regEx.Pattern = ",."

or

regEx.Pattern = "CN="

code is nested here:

Set InputFile = FSO.OpenTextFile("Temp.txt", 1)
Set InputFile = FSO.OpenTextFile("Temp.txt", 1)
set OutPutFile = FSO.OpenTextFile(StrInput & "-Results.txt", 8, True)

do While InputFile.AtEndOfStream = False
    StrLine = InputFile.ReadLine
      If inStr(strLine, TaskChoice) then    
     Set regEx = New RegExp
     regEx.Pattern = "[A-Za-z]{2}=(.+?),.*"
     Result = regEx.Replace(StrLine, "")
     OutputFile.write(Replace(Result,"""","")) & vbCrLf
      End if
2

There are 2 answers

4
arco444 On BEST ANSWER

This should get you started:

str = "CN=APP_GROUP_123,OU=Global Groups,OU=Accounts,DC=corp,DC=contoso,DC=biz"
Set re = New RegExp
re.pattern = "[A-Za-z]{2}=(.+?),.*"

if re.Test(str) then
    set matches = re.Execute(str)
    matched_str = "Matched: " & matches(0).SubMatches(0)
    Wscript.echo matched_str
else
    Wscript.echo "Not a match"
end if

Output:Matched: APP_GROUP_123

The regex you need is [A-Za-z]{2}=(.+?),.*

If the match is successful, it captures everything in the parenthesis. .+? means it will match any character non-greedily up until the first comma. The ? in .+? makes the expression non-greedy. If you were to omit it, you would capture everything up to the final comma at ,DC=biz

2
Ansgar Wiechers On

Your regular expression "[,.*]["CN=]" doesn't work for 2 reasons:

  1. It contains an unescaped double quote. Double quotes inside VBScript strings must be escaped by doubling them, otherwise the interpreter would interpret your expression as a string "[,.*][", followed by an (invalid) variablename CN=] (without an operator too) and the beginning of the next string (the 3rd double quote).

  2. You misunderstand regular expression syntax. Square brackets indicate a character class. An expression [,.*] would match any single comma, period or asterisk, not a comma followed by any number of characters.

    What you meant to use was an alternation, which is expressed by a pipe symbol (|), and the beginning of a string is matched by a caret (^):

     regEx.Pattern = ",.*|^CN="
    

With that said, in your case a better approach would be using a group and replacing the whole string with just the group match:

regEx.Pattern = "^cn=(.*?),.*"
regEx.IgnoreCase = True

Result = regEx.Replace(strLine, "$1")