将 C# 转换为 Powershell 和正则表达式不起作用还是我做错了?



我正在尝试将一个简单的C#类函数转换为Powershell模块,但由于某种原因它不起作用。对我来说,这感觉像是一个Powershell语法问题或怪癖

C#一个工作正常,但不能PowerShell

我正在测试的字符串是ABC123,这在两者中都应该有效。

C# 类库

using System;
using System.Globalization;
using System.Text.RegularExpressions;
namespace KWLabelHelper
{
public class KWLabelHelper
{
public static bool IsValidLabelId(string labelId)
{
if (string.IsNullOrEmpty(labelId))
return false;
if (!KWLabelHelper.Pattern.Matcher_New_LabelID.IsMatch(labelId) && !KWLabelHelper.Pattern.Matcher_Legacy_LabelID.IsMatch(labelId))
return KWLabelHelper.Pattern.Matcher_New_Label_WithLabelFile.IsMatch(labelId);
return true;
}
public static class Pattern
{
public static Regex Matcher_New_LabelID = new Regex("(^[a-zA-Z_])([a-zA-Z\d_])*$",
RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.CultureInvariant);
public static Regex Matcher_Legacy_LabelID = new Regex(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "^{0}{1}{2}$", (object)'@', (object)"[a-zA-Z]\w\w", (object)"\d+"),
RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.CultureInvariant);
public static Regex Matcher_New_Label_WithLabelFile = new Regex("(?<AtSign>\@)(?<LabelFileId>[a-zA-Z]\w*):(?<LabelId>[a-zA-Z]\w*)",
RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.CultureInvariant);
}
}
}

C# 程序.cs

using System;
namespace KWLabelHelperConsole
{
class Program
{
static void Main(string[] args)
{
string labelId = "ABC123"; // VALID
if (KWLabelHelper.KWLabelHelper.IsValidLabelId(labelId))
{
Console.WriteLine("Label '{0}' is valid", labelId);
}
else
{
Console.WriteLine("Label '{0}' is NOT valid", labelId);
}
Console.ReadKey();
}
}
}

电源外壳版本

Clear-Host
function Get-LabelIsValid {
[CmdletBinding()] 
param 
( 
[Parameter(Mandatory=$True, Position=0)] 
[string]$LabelId
)
$RegexOptions = [System.Text.RegularExpressions.RegexOptions]::IgnoreCase -bor [System.Text.RegularExpressions.RegexOptions]::Compiled -bor [System.Text.RegularExpressions.RegexOptions]::CultureInvariant
$Matcher_New_LabelID = New-Object System.Text.RegularExpressions.Regex('(^[a-zA-Z_])([a-zA-Z\d_])*$', $RegexOptions)
$Matcher_Legacy_LabelID = New-Object System.Text.RegularExpressions.Regex([System.String]::Format([System.IFormatProvider][System.Globalization.CultureInfo]::InvariantCulture, "^{0}{1}{2}$", [System.Object]'@', [System.Object]"[a-zA-Z]\w\w", [System.Object]"\d+"), $RegexOptions)
$Matcher_New_Label_WithLabelFile = New-Object System.Text.RegularExpressions.Regex("(?<AtSign>\@)(?<LabelFileId>[a-zA-Z]\w*):(?<LabelId>[a-zA-Z]\w*)", $RegexOptions)
if (!$LabelId)
{
return $false
}
if (!($Matcher_New_LabelID.IsMatch($LabelId)) -and !($Matcher_Legacy_LabelID.IsMatch($LabelId)))
{
return $Matcher_New_Label_WithLabelFile.IsMatch($LabelId)
}
return $True
}
Get-LabelIsValid -LabelId "ABC123" # Should return $True, but returns $False

通过 System.Text.RegularExpressions.Regex(( 使用 Powershell 正则表达式,您无需转义反斜杠。

PS C:> $RegexOptions = [System.Text.RegularExpressions.RegexOptions]::IgnoreCase -bor [System.Text.RegularExpressions.RegexOptions]::Compiled -bor [System.Text.RegularExpressions.RegexOptions]::CultureInvariant
PS C:> $Matcher_New_LabelID = New-Object System.Text.RegularExpressions.Regex('(^[a-zA-Z_])([a-zA-Z\d_])*$', $RegexOptions)
PS C:> $Matcher_Legacy_LabelID = New-Object System.Text.RegularExpressions.Regex([System.String]::Format([System.IFormatProvider][System.Globalization.CultureInfo]::InvariantCulture, "^{0}{1}{2}$", [System.Object]'@', [System.Object]"[a-zA-Z]\w\w", [System.Object]"\d+"), $RegexOptions)
PS C:> $Matcher_New_Label_WithLabelFile = New-Object System.Text.RegularExpressions.Regex("(?<AtSign>\@)(?<LabelFileId>[a-zA-Z]\w*):(?<LabelId>[a-zA-Z]\w*)", $RegexOptions)
PS C:>
PS C:> $LabelId = "ABC123"
PS C:> $Matcher_New_LabelID.IsMatch($LabelId)
False
PS C:> $Matcher_Legacy_LabelID.IsMatch($LabelId)
False
PS C:> $Matcher_New_Label_WithLabelFile.IsMatch($LabelId)
False

Matcher_New_LabelID应该返回 True:

PS C:> $Matcher_New_LabelID = New-Object System.Text.RegularExpressions.Regex("(^[a-zA-Z_])([a-zA-Zd_])*$", $RegexOptions)
PS C:> $Matcher_New_LabelID.IsMatch($LabelId)
True
PS C:> $Matcher_New_LabelID = New-Object System.Text.RegularExpressions.Regex('(^[a-zA-Z_])([a-zA-Zd_])*$', $RegexOptions)
PS C:> $Matcher_New_LabelID.IsMatch($LabelId)
True

最后 3 条正则表达式匹配线

$Matcher_New_LabelID = New-Object System.Text.RegularExpressions.Regex('(^[a-zA-Z_])([a-zA-Zd_])*$', $RegexOptions)
$Matcher_Legacy_LabelID = New-Object System.Text.RegularExpressions.Regex([System.String]::Format([System.IFormatProvider][System.Globalization.CultureInfo]::InvariantCulture, "^{0}{1}{2}$", [System.Object]'@', [System.Object]"[a-zA-Z]ww", [System.Object]"d+"), $RegexOptions)
$Matcher_New_Label_WithLabelFile = New-Object System.Text.RegularExpressions.Regex("(?<AtSign>@)(?<LabelFileId>[a-zA-Z]w*):(?<LabelId>[a-zA-Z]w*)", $RegexOptions)

相关内容

  • 没有找到相关文章

最新更新