Important Notice: On February 29th, this community was put into read-only mode. All existing posts will remain but customers are unable to add new posts or comment on existing. Please feel to join our Community Discord for any questions and discussions.

Collection based on PC name using Regex/pattern matching?

This question is because I admittedly suck at RegEx. Can anyone help me make a collection of PC's that are named anything with a number on the end or -NEW or -OLD? I am trying to clean up a bunch of junk our help desk made that does not follow guidelines.

Bonus if it does not match ##demo# (10demo1) but not required I can work around that.

1

Comments

4 comments
Date Votes
  • 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
  • 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
  • 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
  • Thanks Colby, I didn't realize that regex was case-insensitive in Inventory.

    Glad it worked out for you, Craig.

    0