[英]How return contents of method in txt file
我正在嘗試編寫一個正則表達式來返回方法名稱和以下方法名稱之間的方法內容。 我將進一步解析我正在提取的信息。 我怎么做? 到目前為止,我已經開始從方法名稱中獲取信息,但不知道如何讓它停止在以下方法名稱處。 任何其他想法將不勝感激。
這將返回與字符串方法名稱匹配的行(但我想要整個方法):
$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 )
...
這是我尋找更多信息的地方:
我的問題是如何完全提取該方法名稱的內容? 我不在乎它是否包含以下MethodName,但它需要獲取多行,直到感興趣的方法的結束文本methodNameToReturn。 是的,我知道這樣做很奇怪,但這就是我們所要求的。 這是 PowerShell 5.1。
編輯:我退出了 powershell 文件,看起來它仍在查找文件,但現在它沒有找到任何正則表達式。
首先,要匹配多行,您必須獲取文件的“原始”內容。 Get-Content
默認返回一個字符串列表,但-Raw
為您提供一個可以匹配的字符串:
$contents = Get-Content $codePath -Raw
然后要跨多行進行正則表達式,您必須捕獲行尾字符。 .*
不包括行尾,所以我在 windows 文件上使用[\s\S]*
來代替。 我還從您的示例中添加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 的更多信息
不願編寫成熟的 C++ 解析器,一個有用的啟發式方法是使用方法主體,直到到達對應於開頭{
的關閉}
- 這種方法的好處是PowerShell 的正則表達式運算符可以為您找到它們!
.NET 正則表達式引擎支持稱為平衡組的東西,它允許我們跟蹤我們遇到了多少個左括號,並在每次遇到相應的右括號時遞減計數器:
Select-String -Path $codePath -Pattern "(?s)$methodNameToReturn\s*\(.*\)\s*\{(?:[^{}]|(?<bracket>\{)|(?<-bracket>\}))+(?(bracket)(?!))\}"
當然,您也可以使用 Select-String 來做到這一點,但正則表達式可能只是:
$contents = [regex]::Match($codePath,"(?s)($methodNameToReturn.*\})(.*?$followingMethodName)").Groups[1].Value
可能也可以稍微清理一下正則表達式,但這些捕獲可以滿足您的需求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.