I’m trying to find a function in a few hundred pages and remove it using Powershell. I can match on a single line but I’m having issues getting a multi-line match to work. Any help would be appreciated.
Function I’m trying to find:
Protected Function MyFunction(ByVal ID As Integer) As Boolean
Return list.IsMyFunction()
End Function
Code I’m using that won’t match multi-line:
gci -recurse | ?{$_.Name -match "(?i)MyPage.*\.aspx"} | %{
$c = gc $_.FullName;
if ($c -match "(?m)Protected Function MyFunction\(ByVal ID As Integer\) As Boolean.*End Function") {
$_.Fullname | write-host;
}
}
You can use the
(?s)flag on the regex. S for singleline, also called, dotall in some places, which makes.match across newlines.Also,
gcreads line by line and any comparision / match will be between individual lines and the regex. You will not get a match despite using proper flags on the regex. I usually use[System.IO.File]::ReadAllText()to get the entire file’s contents as a single string.So a working solution will be something like:
For the replace, you can of course use
$matches[0]and use theReplace()method