Test if string contains numeric

How can you test if a string contains a numeric character?

The simple answer is to use a regular expression.  If you’ve been reading my stuff for any length of time you’ll know how much I love regular expressions. This is a simple regex.

Create a string

PS>  $s1 = ‘qwertyuiop’

Test if it has numerics

PS>  $s1 -match ‘\d’

if you use D you’re testing for non numeric characters
PS>  $s1 -match ‘\D’

Lets add a numeric to confirm

PS>  $s2 = ‘qwert6yuiop’
PS>  $s2 -match ‘\d’

This time we get a positive result

The test for non numerics still works
PS>  $s2 -match ‘\D’

This entry was posted in Powershell. Bookmark the permalink.

1 Response to Test if string contains numeric

  1. Andrey Voronin says:

    Is it possible to tell more about .NET? For example, collections (ArrayList, List), StringBuilder, Runspaces, delegates (Func), LINQ…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s