Collection based on PC name using Regex/pattern matching?

Comments

4 comments

  • Luke Nichols

    Try this:

    ^([A-Za-z]\w+)(\d+|-[Nn][Ee][Ww]|-[Oo][Ll][Dd])
    [A-Za-z] matches any letter
    \w matches any alphanumeric character
    \d matches any number
    + is a quantifier saying match 1 or more of the preceding token
    | means OR
    The parentheses are just here to encapsulate a token so the OR logic is more obvious

    - literally matches a hyphen
    [Nn][Ee][Ww] matches any capitalization of "new"
    [Oo][Ll][Dd] matches any capitalization of "old"

    This won't match ##demo# since it won't match anything that begins with a number.

    1
    Comment actions Permalink
  • Colby Bouma

    Regular expressions are case insensitive in Inventory. Here's my submission:

    ^[a-z]\w+(\d+|-(new|old))

    I tested my pattern in PowerShell because Inventory and PowerShell both use .NET regex.

    $TestStrings = @(
    'TEDPC'
    'tedpc-NEW'
    'ZENITH-LT'
    'Accounting3'
    'ceo-old'
    'C3PO'
    '10demo1'
    )
    $Pattern = '^[a-z]\w+(\d+|-(new|old))'

    foreach ( $TestString in $TestStrings ) {

    [PSCustomObject]@{
    'Test String' = $TestString
    'Matches Pattern' = $TestString -match $Pattern
    }

    }

    1
    Comment actions Permalink
  • Craig Mohr

    Awesome thanks guys. I used Colby's because it's a bit easier to read. Works great. Luke's works as well if you add ^ to the beginning otherwise it picks up those demo names. Thanks both of you for the assist!

    1
    Comment actions Permalink
  • Luke Nichols

    Thanks Colby, I didn't realize that regex was case-insensitive in Inventory.

    Glad it worked out for you, Craig.

    0
    Comment actions Permalink

Please sign in to leave a comment.