Pass regex options to PowerShell [regex] type
There are overloads of the static [Regex]::Match()
method that allow to provide the desired [RegexOptions]
programmatically:
# You can combine several options by doing a bitwise or:
$options = [Text.RegularExpressions.RegexOptions]::IgnoreCase -bor [Text.RegularExpressions.RegexOptions]::CultureInvariant
# or by letting casting do the magic:
$options = [Text.RegularExpressions.RegexOptions]'IgnoreCase, CultureInvariant'
$match = [regex]::Match($input, $regex, $options)
Try using -match instead. E.g.,
$minSize = "20Gb"
$regex = "^([0-9]{1,20})(b|kb|mb|gb|tb)$"
$minSize -match $regex #Automatic $Matches variable created
$size=[int64]$Matches[1]
$unit=$Matches[2]
Use PowerShell's -match operator instead. By default it is case-insensitive:
$minSize -match '^([0-9]{1,20})(b|kb|mb|gb|tb)$'
For case-sensitive matches, use -cmatch.