locked
Regular Expression that checks if no numbers in string RRS feed

  • Question

  • I need to check if a string contains numbers anywhere in the value. I've seen quite a few samples out there, but none of them seem to work as expected. This was by far the most common:

    $Test = "First Last"
    $Test2 = "120 W Ave"
    $Test -match "[^0-9]"
    $Test2 -match "[^0-9]" 

    My expectation is that TEST2 would come back as false, since it contains a number.  I have also tried to look for only alpha charaters [A-Za-z], etc.  Not sure what I'm doing wrong here. Would appreciate any help. 


    Levi Stevens | Technical Consultant Dell Services

    Saturday, March 22, 2014 6:12 AM

Answers

  • PS C:\scripts> $Test = "First Last"
    PS C:\scripts> $Test2 = "120 W Ave"
    PS C:\scripts> $Test -notmatch '\d'
    True
    PS C:\scripts> $Test2 -notmatch '\d'
    False
    PS C:\scripts>


    ¯\_(ツ)_/¯

    • Marked as answer by Levi Stevens Saturday, March 22, 2014 6:24 AM
    Saturday, March 22, 2014 6:22 AM

All replies

  • PS C:\scripts> $Test = "First Last"
    PS C:\scripts> $Test2 = "120 W Ave"
    PS C:\scripts> $Test -notmatch '\d'
    True
    PS C:\scripts> $Test2 -notmatch '\d'
    False
    PS C:\scripts>


    ¯\_(ツ)_/¯

    • Marked as answer by Levi Stevens Saturday, March 22, 2014 6:24 AM
    Saturday, March 22, 2014 6:22 AM
  • When did you get so smart? <NEWB>

    Levi Stevens | Technical Consultant Dell Services

    Saturday, March 22, 2014 6:23 AM
  • When did you get so smart? <NEWB>

    Levi Stevens | Technical Consultant Dell Services


    I was born with no brain so they installed a computer.  Now I know everything.

    ¯\_(ツ)_/¯

    Saturday, March 22, 2014 6:25 AM
  • Consider the following.

    ? string contains number
    PS C:\scripts> 'xx3xx' -match '\d'
    True

    ? not (string contains number)
    PS C:\scripts> -not ('xx3xx' -match '\d')
    False

    ? string not contains number
    PS C:\scripts> 'xx3xx' -notmatch '\d'
    False

    The negation an appear anywhere.  It is like multiplication.  It propagates.

    This does not mean the same thing.

    'xx5xx' -match '[^0-9]'

    That asks if any characters is not 0-9.  Only all numeric strings will pass.

    PS C:\scripts> '12245' -match '[^0-9]'
    False
    PS C:\scripts> '122x45' -match '[^0-9]'
    True

    This is actually the inverse of what you wanted.


    ¯\_(ツ)_/¯

    Saturday, March 22, 2014 6:53 AM