I am getting some content from a text file and loading it into a variable.

Once I have the data I iterate through each row of data excluding some values being added into my array like this..

foreach($row in $data)
{
    if($row)
    {
        Write-Host $row
        [Array]$index = $row.Split(" ") -ne "[needWildCardHere]" -ne "Value2" -ne "Value3" -ne "Value4" -ne "Value5"
    }
}

Each value that matches each string I give, will not be added into the array.

Notice my very first value here [Array]$index = $row.Split(" ") -ne "[needWildCardHere]"

I have a bunch of values in the $row of $data that I have that have a timestamp similar to this:

[10:49:32] [09:32:57] [06:17:15] [06:17:15]

Can I put a wild card inbetween brackets so I do not add any value that has [brackets] into my array?

Feel free to ask me if something is unclear,

Thanks

有帮助吗?

解决方案

If you want to use a wildcard match, you need to change from using -ne to -notlike.

If you want to use a regular expression, you'd use -notmatch.

see:

 Get-Help about_comparison_operators

An example:

 [Array]$index = $row.Split(" ") -notlike "*`[*`]*" -ne "Value2" -ne "Value3" -ne "Value4" -ne "Value5"

Note: the square brackets are considered part of the wildcard set, so to match them literally they need to be escaped with backticks.

其他提示

Try -notmatch "\[.*\]" instead of -ne

Personally I'd split and then filter since it seems to give more versatile filtering I think:

$Exclusions = @("value1","value2","value3")
gc c:\temp\test.txt |%{$_.split(" ") |?{$_ -notin $exclusions -and $_ -notmatch "\[.*?]" -and !([string]::IsNullOrWhiteSpace($_))}}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top