如何在文本文件中找到以开方括号开头和以近方括号结尾的所有字符串。 使用了下面的代码,但也得到了一些不需要的内容 。如何避免那些不必要的内容
Select-String -Path "text.txt" -Pattern '\['
输入文件
一些圆锥形 hello
adfhjadf adfjkkf ghad
检查 dfgsf sfgfs
必需的输出文件
你好
加德
检查
发布于 2021-05-24 15:19:28
这里,试试这个:
$string = @'
some conents [hello]
adfhjadf adfjkkf [ghad]
[check] dfgsf sfgfs
([regex]'\[.*?\]').Matches($string)
注意,这将匹配括号内的任何内容。如果你只寻找英文字符,你应该改进正则表达式。
输出应该如下所示:
Groups : {0}
Success : True
Name : 0
Captures : {0}
Index : 13
Length : 7
Value : [hello]
Groups : {0}
Success : True
Name : 0
Captures : {0}
Index : 39
Length : 6
Value : [ghad]
Groups : {0}
Success : True
Name : 0
Captures : {0}
Index : 47