none
Regex

    Question

  • Why is:

    "245622" -match "\d{2,2}"

    true?

    I thought the numbers in the {}-quantifier mean "minimum, maximum"? I want to check if a string contains exactly two numbers.

    Thanks, rob

    Thursday, March 15, 2012 4:52 PM

Answers

  • Why is:

    "245622" -match "\d{2,2}"

    true?

    I thought the numbers in the {}-quantifier mean "minimum, maximum"? I want to check if a string contains exactly two numbers.

    Thanks, rob

    If you look at the results of $Matches, it does indeed match the first two numbers in the string.

    Name                           Value
    ----                           -----
    0                              24

    If you wanted to only validate that the string has 2 characters, I would recommend something like this:

    "245622" -match "^\d{2}$"

    This would put an anchor at the beginning and at the end so only a 2 digit number could be used.

    "24" -match "^\d{2}$"

    Boe Prox

    Please remember to mark the best solution as the answer using Mark as Answer. If you find a solution to be helpful, please use Vote as Helpful.

    Looking for a script? Check out the Script Repository
    Need a script written for you? Submit a request at the Script Request Page




    • Edited by Boe ProxMVP Thursday, March 15, 2012 5:05 PM
    • Proposed as answer by Cyreli Thursday, March 15, 2012 5:13 PM
    • Marked as answer by RobertWe Thursday, March 15, 2012 5:38 PM
    Thursday, March 15, 2012 4:59 PM