如何在Powershell中查找具有特定起始字符和结束字符的特定字符串

如何在Powershell中查找具有特定起始字符和结束字符的特定字符串,powershell,powershell-3.0,powershell-4.0,Powershell,Powershell 3.0,Powershell 4.0,如何在文本文件中查找以开方括号开始并以闭方括号结束的所有字符串。 用于代码下方,但也会获取一些不需要的内容。如何避免这些不需要的内容 Select-String -Path "text.txt" -Pattern '\[' 输入文件: 一些内容[你好] adfhjadf ADFJKF[ghad] [检查]dfgsf sfgfs 所需的输出文件 [你好] [ghad] [检查]在此处,尝试以下操作: $string = @' some conents [hello] adfh

如何在文本文件中查找以开方括号开始并以闭方括号结束的所有字符串。 用于代码下方,但也会获取一些不需要的内容。如何避免这些不需要的内容

Select-String -Path "text.txt" -Pattern '\['
输入文件
一些内容[你好]
adfhjadf ADFJKF[ghad]
[检查]dfgsf sfgfs

所需的输出文件
[你好]
[ghad]
[检查]

在此处,尝试以下操作:

$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
Length   : 7
Value    : [check]
如果您只想查看匹配项,可以执行以下操作:

PS /> ([regex]'\[.*?\]').Matches($string).Value

[hello]
[ghad]
[check]

没有运气,兄弟。。。检查下面的图片@DineshKumar我编辑了我的答案,现在试试工作很好谢谢兄弟。。。。如果我问了一个有价值的问题,请点击向上投票,兄弟:)