将正则表达式选项传递给PowerShell [regex]类型 [英] Pass regex options to PowerShell [regex] type

查看:89
本文介绍了将正则表达式选项传递给PowerShell [regex]类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我捕获了两个使用以下正则表达式代码匹配的组:

I capture two groups matched using the regexp code below:

[regex]$regex = "^([0-9]{1,20})(b|kb|mb|gb|tb)$"

$matches = $regex.match($minSize)

$size=[int64]$matches.Groups[1].Value
$unit=$matches.Groups[2].Value

我的问题是我想使其不区分大小写,并且我不想使用正则表达式修饰符。

My problem is I want to make it case-insensitive, and I do not want to use regex modifiers.

我知道你可以通过.NET中的正则表达式选项,但我无法弄清楚如何使用PowerShell。

I know you can pass regex options in .NET, but I cannot figure out how to do the same with PowerShell.

推荐答案

尝试改用-match。例如,

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]

这篇关于将正则表达式选项传递给PowerShell [regex]类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆