I'm on a phone right now, so I won't be doing much typing..
$str -split '\D+' | Sort-Object -Unique
Should do it.
In addition to this, after the
-split, the elements (although numbers) are still strings. Doing the above sort will therefore sort alphanumeric, not numeric. Since in the example all numbers have three digits, the sort will look fine, but if there are numbers in there with less or more digits, you'll see what I mean:
Lets say your string looks like this:
$str = "123.456,789;12 101-202_303%404..505(606) 7 707 a bunch of text 808 %%%*&#!@#$%^&*() 909"
Splitting and sorting will give you an array like:
101
12
123
202
303
404
456
505
606
7
707
789
808
909
If you change the Sort-Object just a little to become this:
$str -split '\D+' | Sort-Object -Property {[int]$_} -Unique
The output will be sorted Numeric so the result is this:
7
12
101
123
202
303
404
456
505
606
707
789
808
909
Of course, joining the elements after this to become a comma delimited string is as easy as
($str -split '\D+' | Sort-Object -Property {[int]$_} -Unique) -join ','
Output:
7,12,101,123,202,303,404,456,505,606,707,789,808,909
$str -split '\D'.\Dis regex for "anything that is not a digit". If you suspect more than one non-digit in between, use\D+-split '\D' | Where-Object{$_ -ne ""} | sort -Unique. Feel free to make that an answer.