0

I've found multiple examples of what I'm trying here, but for some reason it's not working.

I have a list of regular expressions that I'm checking against a single value and I can't seem to get a match.

I'm attempting to match domains. e.g. gmail.com, yahoo.com, live.com, etc.

I am importing a csv to get the domains and have debugged this code to make sure the values are what I expect. e.g. "gmail.com"

Regular expression examples AKA $FinalWhiteListArray

(?i)gmail\.com
(?i)yahoo\.com
(?i)live\.com

Code

Function CheckDirectoryForCSVFilesToSearch {

    $global:CSVFiles = Get-ChildItem $Global:Directory -recurse -Include *.csv | % {$_.FullName} #removed -recurse
}

Function ImportCSVReports {
    Foreach ($CurrentChangeReport in $global:CSVFiles) {
        $global:ImportedChangeReport = Import-csv $CurrentChangeReport
    }
}
Function CreateWhiteListArrayNOREGEX {
    $Global:FinalWhiteListArray = New-Object System.Collections.ArrayList
    $WhiteListPath = $Global:ScriptRootDir + "\" + "WhiteList.txt"
    $Global:FinalWhiteListArray= Get-Content $WhiteListPath
}
$Global:ScriptRootDir = Split-Path -Path $psISE.CurrentFile.FullPath
$Global:Directory = $Global:ScriptRootDir + "\" + "Reports to Search" + "\" #Where to search for CSV files

CheckDirectoryForCSVFilesToSearch
ImportCSVReports
CreateWhiteListArrayNOREGEX

Foreach ($Global:Change in $global:ImportedChangeReport){
    If (-not ([string]::IsNullOrEmpty($Global:Change.Previous_Provider_Contact_Email))){
       $pos = $Global:Change.Provider_Contact_Email.IndexOf("@")
       $leftPart = $Global:Change.Provider_Contact_Email.Substring(0, $pos)
       $Global:Domain = $Global:Change.Provider_Contact_Email.Substring($pos+1)

       $results = $Global:FinalWhiteListArray | Where-Object { $_ -match $global:Domain}
}

}

Thanks in advance for any help with this.

8
  • 2
    what is in each of $ListOfDomains & $FinalWhiteListArray? Commented Dec 14, 2018 at 16:34
  • Edited the post to be more descriptive. Commented Dec 14, 2018 at 16:39
  • 1
    As the -match operator is RegEx based: $ListOfDomains = @("gmail\.com","yahoo\.com","live\.com") or using alternation $ListOfDomains = "gmail\.com|yahoo\.com|live\.com" BTW the operators in PowerShell are by default case insensitive, so the (?i)` isn't necessary. ` Commented Dec 14, 2018 at 16:42
  • 1
    @MrMr - thanks! now i can tell that you have your regex on the wrong side of the -match operator. [grin] try swapping those two and you otta get your matches. ///// i agree with LotPings that you have no need for the (?i) stuff, AND that you likely otta use a regex OR of those regex patterns. Commented Dec 14, 2018 at 16:55
  • 1
    @MrMr - kool! thanks for the reminder ... and i'm glad you got it working as needed! [grin] Commented Dec 14, 2018 at 17:18

1 Answer 1

1

the problem with your current code is that you put the regex on the left side of the -match operator. [grin] swap that and your code otta work.

taking into account what LotPings pointed out about case sensitivity and using a regex OR symbol to make one test per URL, here's a demo of some of that. the \b is for word boundaries, the | is the regex OR symbol. the $RegexURL_WhiteList section builds that regex pattern from the 1st array. if i haven't made something clear, please ask ...

$URL_WhiteList = @(
    'gmail.com'
    'yahoo.com'
    'live.com'
    )
$RegexURL_WhiteList = -join @('\b' ,(@($URL_WhiteList |
    ForEach-Object {
        [regex]::Escape($_)
        }) -join '|\b'))

$NeedFiltering = @(
    'example.com/this/that'
    'GMail.com'
    'gmailstuff.org/NothingElse'
    'NotReallyYahoo.com'
    'www.yahoo.com'
    'SomewhereFarAway.net/maybe/not/yet'
    'live.net'
    'Live.com/other/another'
    )

foreach ($NF_Item in $NeedFiltering)
    {
    if ($NF_Item -match $RegexURL_WhiteList)
        {
        '[ {0} ] matched one of the test URLs.' -f $NF_Item 
        }
    }

output ...

[ GMail.com ] matched one of the test URLs.
[ www.yahoo.com ] matched one of the test URLs.
[ Live.com/other/another ] matched one of the test URLs.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.