powershell - 从变量获取匹配的字数

标签 powershell powershell-5.0

我想在test变量中获取$channel单词的总数,但是$a返回的内容与$channel相同。

$channel = "test1 test2 test3 test4 testIgnore1 testIgnore2 testIgnore3"
$a = Select-String -InputObject $channel -Pattern "test" 

此处的确切解决方案是什么?

最佳答案

$channel = "test1 test2 test3 test4 testIgnore1 testIgnore2 testIgnore3"
([regex]::Matches($channel, "test" )).count

将为您提供比赛的次数:7

关于powershell - 从变量获取匹配的字数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43182567/

相关文章:

git - 为什么ls在GitShell中工作,而ls -a或ls -la无法工作?

powershell - 在数组中查找字符串 Powershell V2.0

powershell - 根据长度递归重命名文件夹

windows - 在 Visual Studio 中将 PowerShell 脚本作为构建事件运行的最简单/最短命令

powershell - 在Powershell脚本中捕获错误

powershell - 在 powershell 中安装永久驱动器

powershell - 如何关闭控制台中的语法高亮?

windows - 未找到内置 DSC 资源

powershell - 可以使用Write-Error或仅使用throw指定错误类型吗?

powershell - 使用 powershell 将 SQL Azure 数据库设置为只读