Use of -match

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 7 months ago.

  • Author
    Posts
  • #5666

    by rambog at 2013-03-15 06:57:51

    I am trying to do a simple comparison. At the 6th character of a string, I am trying to determine if it contains the integer "1". There seems to a plethora of examples on how to perform more complex matches by issuing the command "help about_reg*" in powershell or consulting http://msdn.microsoft.com/library/az24scfc.aspx. Nothing is evident for my instance in all these examples and I cannot seem to derive if from the examples given. I have tried -match ".....1*" but when I peform the -match ".....2*" it gives me the same result. Please let me know how I can specify a match on a particular integer which would be the 6th character in a string.

    by MasterOfTheHat at 2013-03-15 07:06:27

    Since you are looking for a specific character at a specific location in a string, just treat the string like an array:
    PS C:\Windows> $str = "abcde1ghi"
    PS C:\Windows> $str[5] -eq "1"
    True
    PS C:\Windows> $str[5] -eq "2"
    False

    by mjolinor at 2013-03-15 07:13:50

    Just for reference, a regex for that might look like this:
    $string = 'abcde1ghi'
    $string -match '^.{5}1'

You must be logged in to reply to this topic.