简体   繁体   中英

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. It feels like a Powershell syntax issue or quirk to me.

The C# one works fine but not the PowerShell .

The string I'm testing with is ABC123 , which should be VALID in both.

C# Class Library

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# Program.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();
        }
    }
}

Powershell Version

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.

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:

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

Final 3 Regex Matcher Lines

$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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM