我正在嘗試撰寫一個正則運算式來回傳方法名稱和以下方法名稱之間的方法內容。我將進一步決議我正在提取的資訊。我怎么做?到目前為止,我可以從方法名稱中獲取資訊,但不知道如何讓它停止在以下方法名稱處。任何其他想法將不勝感激。
這將回傳行小號相匹配的字串的方法名稱(但我想整個方法):
$contents = Get-Content $codePath | Select-String -Pattern "$methodNameToReturn.*" -AllMatches
這是我試圖讓它結束在以下方法中回傳的字串的地方,但它沒有找到任何東西:
Get-MethodContents -codePath $File[0] -methodNameToReturn "GetStatusFromCode" -followingMethodName "SkipPage"
Function Get-MethodContents{
[cmdletbinding()]
Param ( [string]$codePath, [string]$methodNameToReturn, [string]$followingMethodName)
Process
{
$contents = ""
Write-Host "In GetMethodContents method File:$codePath method:$methodNameToReturn followingMethod:$followingMethodName" -ForegroundColor Green
#Switch -regex (Get-Content -Path $codePath)
#{
#'^(.*)/s'{$contents=$switch.current}
#}
$contents = Get-Content $codePath | Select-String -Pattern "($methodNameToReturn.*$followingMethodName)" -AllMatches
Write-Host "File Contents Found: $contents" -ForegroundColor Cyan
}#End of Process
}#End of Function
這是嘗試匹配的示例代碼(重要的是我得到了 GetStatusFromCode 方法中的內容以進一步決議):
...
bool ClassName::GetStatusFromCode( DWORD errorCode,
TCHAR *errorStr,
DWORD &outError,
COM_ERROR_SEVERITY &outSeverity,
TCHAR *outDevStr)
{
m_bRestart = TRUE;
BYTE MinorCode = 0;
// Get and check width
DWORD dwLength = 0;
BYTE buff[ 2 ] = {0};
//
if ( chcusb_getInfo( (WORD)kPINFTAG_SVCINFO, buff, &dwLength ) == FALSE )
{
// Error
}
MinorCode = buff[1];
switch( errorCode )
{
//case kRESULT_NOERROR:
case kRESULT_MEMFULLERR:
//code
_sprintf(outDevStr, _T("8000 - (Comm Err)"), errStr);
outError = errVar;
break;
case kRESULT_USBNOTFOUND:
//code
_sprintf(outDevStr, _T("8001 - (Comm 1 Err)"), errStr);
outError = errVar;
break;
default:
// Maybe communication error
break;
}
m_dwErrorCode = outError;
if ( m_bRestart == TRUE )
{
return true;
}
else
{
return false;
}
}
////////////////////////////////////////
//
// METHOD NAME: SkipPage
//
////////////////////////////////////////
bool ClassName::SkipPage( GUID JobID, DWORD dwPageNum )
...
這是我尋找更多資訊的地方:
多行匹配字符
提取文本檔案中的字串
My question is how do I extract contents of that method name entirely? I don't care if it includes the followingMethodName, but it needs to get multiple lines until the end text of the method of interest, methodNameToReturn. Yes, I know this is weird to want to do, but it's what is being asked for. This is PowerShell 5.1.
Edit: I exited the powershell file and it looks like it's finding the files still, but now it's not finding anything with either regex.
uj5u.com熱心網友回復:
首先,要匹配多行,您必須獲取檔案的“原始”內容。Get-Content默認情況下回傳一個字串串列,但-Raw為您提供一個可以匹配的字串:
$contents = Get-Content $codePath -Raw
然后要跨多行進行正則運算式,您必須捕獲行尾字符。.*不包括行尾,所以我[\s\S]*在 Windows 檔案上使用其中之一。我還Classname::從您的示例中添加了內容,因為它似乎是開始/結束的好地方:
# -match usually returns true/false, so suppress
$null = $contents -match "(Classname::$methodNameToReturn[\s\S]*)Classname::$followingMethodName"
# Return the contents of the first capture group () as a single string
$Matches.Item(1)
有關-Match 的更多資訊
uj5u.com熱心網友回復:
避免撰寫成熟的 C 決議器,一個有用的啟發式方法是使用方法主體,直到到達}與開頭相對應的結尾{——這種方法的好處是PowerShell 的正則運算式運算子可以為你找到它們!
.NET 正則運算式引擎支持稱為平衡組的東西,它允許我們跟蹤遇到的左括號的數量,并在每次遇到相應的右括號時遞減計數器:
Select-String -Path $codePath -Pattern "(?s)$methodNameToReturn\s*\(.*\)\s*\{(?:[^{}]|(?<bracket>\{)|(?<-bracket>\})) (?(bracket)(?!))\}"
uj5u.com熱心網友回復:
當然,您也可以使用 Select-String 來做到這一點,但正則運算式可能只是:
$contents = [regex]::Match($codePath,"(?s)($methodNameToReturn.*\})(.*?$followingMethodName)").Groups[1].Value
可能也可以稍微清理一下正則運算式,但是這些捕獲可以滿足您的需求。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/381811.html
標籤:regex powershell
下一篇:并行運行一個函式并等待輸出
