Maxsteel
Maxsteel

Reputation: 2040

Print function name in which a pattern of string occurs

I am looking for a way to automate a manual task. I'm not sure if it's even possible.

I have to find a pattern of string in all files in a project folder. It'a project of C#/.net project(if that matters at all). I have to also write the function name and file name where the pattern occurs, along with the full string that matches it. So far I've done following in PowerShell:

PS C:\trunk> Get-ChildItem "C:\trunk” -recurse | Select-String -pattern
“AlertMessage” | group path | select name

This prints file name where string pattern matches.

PS C:\trunk> Select-String -pattern "AlertMessage" -path
"C:\trunk\VATScan.Web\Areas \Administration\Controllers\HomeController.cs”

This prints line number and string that matches it in a given file.

Any pointers on how I can acheive my goal?

Upvotes: 0

Views: 73

Answers (1)

Matt
Matt

Reputation: 46710

By no means perfect but at least this my fall under the category of pointer

$text = @"
Public Sub Bitchin()
    Dim AlertMe
End Sub

Private Sub Function() As something

End Function
"@

[void]($text -match "(?smi)((public|private)\W(sub|function)\W(.+?)\(.*?Alertme)")
$Matches[4]

This will look for a Function or Sub routine declaration with a single white space between words followed by the next occurrence of the word AlertMe

Need to get item 4 from $Matches since there are a bunch of capture groups.

A more concise explanation of the regex used can be found here

Hopefully this will get you started or at least thinking. I am not familiar with c# declarations as $text is more of a VBA example but your should get the idea.

Upvotes: 1

Related Questions