Search code examples
regexvbscriptdsquery

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


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

Solution

  • 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