0

I have a variable list of number (switch ports) that I need to build into a string that indicates which ports should be "included" and which should be "excluded". Here is an example of the required format:

(Port 1[!(0-2 | 4-9)] | Port 4[!(0-3 | 5)])*

In this example, if my numbers are 1-19 and 40-45, then Port 1, Port 3, and Port 44 will be included while ports 10-12, 14-19, 40-43, and 45 will be excluded.

I do not know, in advance, what numbers will be present in the port list. How can I build this expression?

StackExchangeGuy
  • 741
  • 16
  • 36

1 Answers1

1

If I understand this correctly as a regex... 1-9 and 20-39 are other cases. So include 1-9,13,20-39,44.

1..9 + 13 + 20..39 + 44 | % tostring 'port 0' | 
  select-string 'port [1-9]$|port 1[^0-2,4-9]|port [2-3][0-9]|port 4[^0-3,5]'

port 1
port 2
port 3
...


10..12 + 14..19 + 40..43 + 45 | % tostring 'port 0' | 
  select-string 'port [1-9]$|port 1[^0-2,4-9]|port [2-3][0-9]|port 4[^0-3,5]'
# no output

Or just check if a string is in a set:

$include = 1..9 + 13 + 20..39 + 44 | % tostring 'port 0'
1..45 | % tostring 'port 0' | where { $_ -in $include }

port 1
port 2
port 3
...
js2010
  • 23,033
  • 6
  • 64
  • 66