Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 用于查找和删除函数的Powershell正则表达式_Regex_Powershell - Fatal编程技术网

Regex 用于查找和删除函数的Powershell正则表达式

Regex 用于查找和删除函数的Powershell正则表达式,regex,powershell,Regex,Powershell,我试图在几百页中找到一个函数,并使用Powershell将其删除。我可以在一条线上进行匹配,但我在多条线上进行匹配时遇到了问题。任何帮助都将不胜感激 我试图找到的函数: Protected Function MyFunction(ByVal ID As Integer) As Boolean Return list.IsMyFunction() End Function 我使用的代码与多行代码不匹配: gci -recurse | ?{$_.Name -match "(?i)MyP

我试图在几百页中找到一个函数,并使用Powershell将其删除。我可以在一条线上进行匹配,但我在多条线上进行匹配时遇到了问题。任何帮助都将不胜感激

我试图找到的函数:

Protected Function MyFunction(ByVal ID As Integer) As Boolean
    Return list.IsMyFunction()  
End Function
我使用的代码与多行代码不匹配:

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

默认情况下,-match运算符将不会通过回车搜索。*。您需要直接使用.Net Regex.Match函数来指定“singleline”(在本例中不幸命名)搜索选项:

[Regex]::Match($c,
               "(?m)Protected Function MyFunction\(ByVal ID As Integer\) As Boolean.*End Function", 
               'Singleline')

有关更多详细信息,请参阅MSDN中的函数和。

默认情况下,-match运算符将不会通过回车搜索。*。您需要直接使用.Net Regex.Match函数来指定“singleline”(在本例中不幸命名)搜索选项:

[Regex]::Match($c,
               "(?m)Protected Function MyFunction\(ByVal ID As Integer\) As Boolean.*End Function", 
               'Singleline')
有关更多详细信息,请参阅MSDN中的函数和。

您可以使用正则表达式上的
(?s)
标志。S表示单线,在某些地方也称为dotall,这使得
在新行之间匹配

另外,
gc
逐行读取,任何比较/匹配都将在各行和正则表达式之间进行。即使在正则表达式上使用了正确的标志,也不会得到匹配。我通常使用
[System.IO.File]::ReadAllText()
以单个字符串的形式获取整个文件的内容

因此,有效的解决方案如下:

gci -recurse | ?{$_.Name -match "(?i)MyPage.*\.aspx"} | %{
  $c = [System.IO.File]::ReadAllText($_.Fullname)
  if ($c -match "(?s)Protected Function MyFunction\(ByVal ID As Integer\) As Boolean.*End Function")  {
    $_.Fullname | write-host;
  }

}
对于替换,您当然可以使用
$matches[0]
并使用
replace()
方法

$newc = $c.Replace($matches[0],"")
您可以在正则表达式上使用
(?s)
标志。S表示单线,在某些地方也称为dotall,这使得
在新行之间匹配

另外,
gc
逐行读取,任何比较/匹配都将在各行和正则表达式之间进行。即使在正则表达式上使用了正确的标志,也不会得到匹配。我通常使用
[System.IO.File]::ReadAllText()
以单个字符串的形式获取整个文件的内容

因此,有效的解决方案如下:

gci -recurse | ?{$_.Name -match "(?i)MyPage.*\.aspx"} | %{
  $c = [System.IO.File]::ReadAllText($_.Fullname)
  if ($c -match "(?s)Protected Function MyFunction\(ByVal ID As Integer\) As Boolean.*End Function")  {
    $_.Fullname | write-host;
  }

}
对于替换,您当然可以使用
$matches[0]
并使用
replace()
方法

$newc = $c.Replace($matches[0],"")

这个解决方案非常适合搜索。我没有选择它作为答案的原因是我当时在写内容时遇到了问题。使用.net[regex]::replace函数将去掉换行符,而我标记为答案的解决方案修复了搜索问题,并保留了集合内容的格式。有关更多详细信息,请再次参阅“谢谢”。此解决方案非常适合搜索。我没有选择它作为答案的原因是我当时在写内容时遇到了问题。使用.net[regex]::replace函数将去掉换行符,而我标记为答案的解决方案修复了搜索问题,并保留了集合内容的格式。有关更多详细信息,请再次参阅“谢谢”。