[英]Converting C# to Powershell and Regex is not working or am I doing it wrong?
I'm trying to convert a simple C#
class function into a Powershell
module and for some reason it's not working.我正在尝试将一个简单的C#
类函数转换为Powershell
模块,但由于某种原因它无法正常工作。 It feels like a Powershell
syntax issue or quirk to me.对我来说,这感觉像是一个Powershell
语法问题或怪癖。
The C#
one works fine but not the PowerShell
. C#
一个工作正常但不是PowerShell
。
The string I'm testing with is ABC123
, which should be VALID in both.我正在测试的字符串是ABC123
,它在两者中都应该是 VALID 。
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);
}
}
}
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
Using Powershell regex via System.Text.RegularExpressions.Regex(), you don't need to escape backslashes.通过 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 should be returning True: Matcher_New_LabelID 应该返回 True:
PS C:\> $Matcher_New_LabelID = New-Object System.Text.RegularExpressions.Regex("(^[a-zA-Z_])([a-zA-Z\d_])*$", $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-Z\d_])*$', $RegexOptions)
PS C:\> $Matcher_New_LabelID.IsMatch($LabelId)
True
$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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.