我想获取下面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-Content
和 Select-String
来完成此操作,但仍然无法获得所需的输出。
最佳答案
使用 Get-Content
cmdlet 读取您的文件并使用 regex
获取您所需的内容:
$content = Get-Content 'your_file_path' -raw
$matches = [regex]::Matches($content, '"\$ctrl\.([^"]+)')
$matches | ForEach-Object {
$_.Groups[1].Value
}
正则表达式:
"\$ctrl\.[^"]+
输出:
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/