locked
Regex to verify a PO Number RRS feed

  • Question

  • OK, I have a bit of an issue.

    I have a directory that I need to start filtering some files on.

    I thought I had my script working perfectly, based on what they originally told me the requirements were, then they threw a wrench at me, I mean into it.

    These file names are based on Purchase Order Numbers.  First they told me that the PO Numbers would be a minimum of 7 alphanumeric characters.  Simple

    Now, for the PO part of the file they are telling me...
    MUST be at least 7 characters - CAN be alphanumeric - CANNOT contain 3 or more consecutive letters

    I'm not sure how to do the 2 letter max

    123AB67 - would be a match

    123ABC7 - would fail

    Is this possible?  If so, can someone point me in the right direction?

    Thanks and have a Great Weekend

    Friday, April 4, 2014 8:37 PM

Answers

  • Are there extensions on these files? Anything else in the file name besides the PO number?

    Assuming that the answers to those questions are "yes, there are extensions" and "no, the entire file name is the PO number", here's an example.  I'd probably separate this into two regular expressions, to save my sanity:

    Get-ChildItem -Path C:\Some\Directory -File |
    Where-Object { $_.Name -match '^([A-Z0-9]{7,})(?:\.[^\.]*)?$' -and $matches[1] -notmatch '[A-Z]{3,}' }

    The first pattern matches any file name (ignoring the extension) which consists of at least 7 alphanumeric characters. The second pattern makes sure there are not 3 consecutive letters in the name, if it passes the first pattern.


    • Edited by David Wyatt Saturday, April 5, 2014 12:20 AM
    • Marked as answer by Mike here Monday, April 7, 2014 3:12 PM
    Saturday, April 5, 2014 12:20 AM

All replies

  • Are there extensions on these files? Anything else in the file name besides the PO number?

    Assuming that the answers to those questions are "yes, there are extensions" and "no, the entire file name is the PO number", here's an example.  I'd probably separate this into two regular expressions, to save my sanity:

    Get-ChildItem -Path C:\Some\Directory -File |
    Where-Object { $_.Name -match '^([A-Z0-9]{7,})(?:\.[^\.]*)?$' -and $matches[1] -notmatch '[A-Z]{3,}' }

    The first pattern matches any file name (ignoring the extension) which consists of at least 7 alphanumeric characters. The second pattern makes sure there are not 3 consecutive letters in the name, if it passes the first pattern.


    • Edited by David Wyatt Saturday, April 5, 2014 12:20 AM
    • Marked as answer by Mike here Monday, April 7, 2014 3:12 PM
    Saturday, April 5, 2014 12:20 AM
  • They're actually PDF files.

    I'm doing through a Switch statement (there are other parts to the filename). and what you have given me is going to do exactly what I need it to do.

    Thanks

    Monday, April 7, 2014 3:11 PM