2

I have an array with folder names. I want to loop thru this array and find out if an entry is only 7 in length and only contain numbers.

Can anyone please push me in the correct direction? Thanks!

1
  • 3
    $array -match '^\d{7}$' Commented Apr 30, 2015 at 7:07

2 Answers 2

2

One solution is while looping through the array names check if the two conditions are met like so:

foreach ($name in "test", "1234567", "test02", "001") {
    if ($name.Length -eq 7 -and $name -match '^\d+$'){
        Write-Host $name
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I just came across this answer and it helped me. But I don't quite understand what '^\d+$' means. Why does this work?
@gwynbleidd that is the regular expression that covers the OP requirement for "capturing" numbers only. We have the two position anchors: leading (^) and trailing ($) and between one or more (+) decimals (\d). Hope this helps!
2

As far as i understood your question :

function Is-Numeric ($Value)
{
    return $Value -match "^[\d\.]+$"
}

$birds = "owl","crow","robin","wren","jay","123"
foreach ($bird in $birds) { if($bird.length -eq 3 -and (Is-Numeric $bird)) {"$bird"} }

just switch it to your case :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.