mpalatsi
mpalatsi

Reputation: 33

Only identify strings with upper AND lowercase

I would like to use powershell to identify only strings that contain both upper and lowercase characters.

$param = "Sam"

If ($param -cmatch "[A-Z]"){
    Write-Host "String has uppercase characters"
}

This is what I have right now, but this only returns when uppercase characters exist in the string. I would like for it to return ONLY if both exist in the same string.

Upvotes: 1

Views: 347

Answers (3)

JPlatts
JPlatts

Reputation: 39

Try

$param -cmatch "[A-Z]*.[a-z]" -or $param -cmatch "[a-z]*.[A-Z]"

You can try different patterns at http://regexstorm.net/tester

(Thanks to briantist and Keith Thompson for the updated patterns.)

Upvotes: 1

rock321987
rock321987

Reputation: 11042

Lookaheads are supported in powershell. So you can use this regex

^(?=.*[A-Z])(?=.*[a-z]).*$

Regex Demo

Powershell Code

If ($param -cmatch "^(?=.*[A-Z])(?=.*[a-z]).*$") { Write-Host "String has both upper and lowercase characters" }

Upvotes: 0

briantist
briantist

Reputation: 47872

I would use:

if ($param -cmatch '[a-z]' -and $param -cmatch '[A-Z]')

It has to satisfy both matches, a single lowercase character somewhere in the string and a single uppercase character somewhere in the string.

Upvotes: 1

Related Questions