我有一个数字(交换机端口)的变量列表,我需要将其构建到一个字符串中,该字符串指示哪些端口应该“包括”,哪些端口应该“排除”。以下是所需格式的示例:
(Port 1[!(0-2 | 4-9)] | Port 4[!(0-3 | 5)])*
在本例中,如果我的号码是1-19和40- 45,则端口1、端口3和端口44将包括在内,而端口10-12、14-19、40-43和45将被排除。
我事先不知道端口列表中会出现哪些数字。我如何构建这个表达式?
发布于 2021-11-13 15:26:10
如果我把它理解为正则表达式的话...1-9和20-39是其他情况。因此包括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
或者只是检查字符串是否在集合中:
$include = 1..9 + 13 + 20..39 + 44 | % tostring 'port 0'
1..45 | % tostring 'port 0' | where { $_ -in $include }
port 1
port 2
port 3
...
https://stackoverflow.com/questions/69948413
复制相似问题