none
Need help with a regular expression RRS feed

  • Question

  • Hello everyone,

    I'm trying to modify a regular expression which I made in the past:

    "^[A-Z]{1}((?<s>[1-9]{2})|(?<l>[1-9]{4}))\\.?(?(s)([0-9]{2})|([0-9]{3}))[A-Z]?$"

    This regex was used to match the following user inputs:

    A1100
    A1100A
    A11.00
    A11.00A
    A1111000
    A1111000A
    A1111.000
    A1111.000A

    A basic break down of the requirements:

    Input must start with a letter (= [A-Z]{1})
    The letter can be followed by either 2 digits from 1-9 (=short version) or 4 digits [1-9] (= standard version)
    The dot seperator is optional
    After the first group of digits, depending on which form was used, we either need to have 2 digits (= short version) or 3 digits (= long version) [0-9]
    At the end, an optional letter can occurr.

    Now, this regex was working fine, untill the mask for the standard version changed.

    It went from A1111.000A to A11.11.000A

    I've tried modifying it like this, but it doesn't work at all anymore :(

    "^[A-Z]{1}((?<s>[1-9]{2})|(?<l>([1-9]{2}\\.?[1-9]{2})))\\.?(?(s)([0-9]{2})|([0-9]{3}))[A-Z]?$"

    Can anyone shed some light on this?

    Thanks!


    Kind regards,

    Dirk Schuermans
    Microsoft Student Partner

    Monday, October 21, 2013 10:03 AM

Answers

  • Hello everyone,

    I'm trying to modify a regular expression which I made in the past:

    "^[A-Z]{1}((?<s>[1-9]{2})|(?<l>[1-9]{4}))\\.?(?(s)([0-9]{2})|([0-9]{3}))[A-Z]?$"

    This regex was used to match the following user inputs:

    A1100
    A1100A
    A11.00
    A11.00A
    A1111000
    A1111000A
    A1111.000
    A1111.000A

    A basic break down of the requirements:

    Input must start with a letter (= [A-Z]{1})
    The letter can be followed by either 2 digits from 1-9 (=short version) or 4 digits [1-9] (= standard version)
    The dot seperator is optional
    After the first group of digits, depending on which form was used, we either need to have 2 digits (= short version) or 3 digits (= long version) [0-9]
    At the end, an optional letter can occurr.

    Now, this regex was working fine, untill the mask for the standard version changed.

    It went from A1111.000A to A11.11.000A

    I've tried modifying it like this, but it doesn't work at all anymore :(

    "^[A-Z]{1}((?<s>[1-9]{2})|(?<l>([1-9]{2}\\.?[1-9]{2})))\\.?(?(s)([0-9]{2})|([0-9]{3}))[A-Z]?$"

    Can anyone shed some light on this?

    Thanks!


    Kind regards,

    Dirk Schuermans
    Microsoft Student Partner

    Hmpf, after an hour of trial and error I managed to solve it myself :x

    Turns out I was testing with a wrong input -.-'
    The regex I provided was correct


    Kind regards,<br/> <br/> Dirk Schuermans

    Monday, October 21, 2013 11:32 AM