2

I want to get the string which are there after $ctrl in below html code snippet:

 <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>

So I am trying to get output like:

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

I am trying to do it using Get-Content and Select-String but still not able to get the desired output.

1 Answer 1

4

Use the Get-Content cmdlet to read your file and use a regex to fetch your desired content:

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

Regex:

"\$ctrl\.[^"]+

Regular expression visualization

Output:

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

Another approach using the Select-String cmdlet and a regex with positive lookbehind:

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

Output:

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

Note: This will only return the first $ctrl.* match of each line. But since this matches your desired output it could be usefull for you.

Sign up to request clarification or add additional context in comments.

1 Comment

what if I want all the matches on a line?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.