powershell - 使用 PowerShell 获取模式后的字符串值

标签 powershell

我想获取下面html代码片段中$ctrl之后的字符串:

 <div ng-if="$ctrl.CvReportModel.IsReady">
                              ng-click="$ctrl.confirmation()"></cs-page-btn>
                 <cs-field field="$ctrl.CvReportModel.Product" ng-model="$ctrl.UploadedFile.Product"></cs-field>
                 <cs-field field="$ctrl.CvReportModel.Month" ng-model="$ctrl.UploadedFile.Month"></cs-field>

所以我试图获得如下输出:

CvReportModel.IsReady
confirmation()
CvReportModel.Product
CvReportModel.Month

我尝试使用 Get-ContentSelect-String 来完成此操作,但仍然无法获得所需的输出。

最佳答案

使用 Get-Content cmdlet 读取您的文件并使用 regex 获取您所需的内容:

$content = Get-Content 'your_file_path' -raw
$matches = [regex]::Matches($content, '"\$ctrl\.([^"]+)')
$matches | ForEach-Object {
    $_.Groups[1].Value
}

正则表达式:

"\$ctrl\.[^"]+

Regular expression visualization

输出:

CvReportModel.IsReady
confirmation()
CvReportModel.Product
UploadedFile.Product
CvReportModel.Month
UploadedFile.Month

使用Select-String cmdlet 和具有正向lookbehind 的regex 的另一种方法:

Select-String -Path $scripts.tmp -Pattern '(?<=\$ctrl\.)[^"]+' | 
    ForEach-Object { $_.Matches.Value }

输出:

CvReportModel.IsReady
confirmation()
CvReportModel.Product
CvReportModel.Month

注意: 这只会返回每行的第一个 $ctrl.* 匹配项。但由于这符合您想要的输出,因此它可能对您有用。

关于powershell - 使用 PowerShell 获取模式后的字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38376680/

相关文章:

excel - 如何在powershell中读取Excel文件?

c# - 如何将 4 字节字符串编码为单个 32 位整数?

powershell - Powershell 可以用来列出 URL 目录的内容吗?

powershell - 如果代码的其他部分或其他点源脚本中未定义,如何检查然后定义函数

string - 在Powershell中迭代字符串的通用列表(Lync聊天室成员)

powershell - DSC 文件资源 - 复制文件

powershell - 如果我使用 Start-Process 在 Powershell 中启动一个进程,如何检查它是否已完成

Powershell获取当前域名或计算机名\用户以及两者之间的区别

azure - 通过 Powershell Runbook 从 ARM 模板部署 Azure VM,无需下载模板

powershell - 如何删除文件名中括号内的所有字符串?