Reputation:
I am trying to use regular expressions to match against a string that starts with 7 numbers, then has a "K" inbetween it, and then 3 numbers again. For example: 1234567K890.
I currently have $_a -match '^\d{7}K\d{3}'
. However, this does not work for my purposes. Does anyone have a solution?
Upvotes: 1
Views: 109
Reputation: 9854
PS C:\> "1234567K890" -match "\d{7}(k)\d{3}"
This \d{7}
matches 7 digits then (k)
matches letter k and \d{3}
matches last three characters.
Upvotes: 2
Reputation: 26023
Tested this, works for your example and some others:
$string = "1234567K890"
$string -match '^[0-9]{7}(k)[0-9]{3}$'"
It matches against exactly 7 numbers, then against K (casing does not matter), then against exactly 3 numbers. The characters at the beginning and the end of the string restrict against whitespace at the beginning and end of the string -- if you want whitespace to be allowed, you can just remove them.
Here's a powershell regex reference, which may help in the future.
Upvotes: 2