I have several regular expression blocks that parse a c++ file for certain info. I'm trying to change my regex so that it avoids the commented blocks. The code that still captures the commented block is:
Function Get-CaseContents{
[cmdletbinding()]
Param ( [string]$parsedCaseMethod, [string]$basePathFull)
Process
{
# split into separate "case" blocks.
# (the funky "(?=...)" preserves the delimiter)
$blocks = $parsedCaseMethod -split "(?=case (.*):)";
$pattern = `
"_stprintf[\s\S]*?_T\D*" +
"(?<sdkErr>[x\d]+)" +
"\D[\s\S]*?" +
"\((?<sdkDesc>(.+?)`")\)" +
"[\s\S]*?" +
"(outError\s*=\s*(?<sdkOutErr>[a-zA-Z_0-9]*))" +
"[\s\S]*?" +
"(?<sdkSeverity>outSeverity\s*=\s[a-zA-Z_]*)";
# note - skip first block as it's the preamble before the first "if"
$result = $blocks `
| select-object -skip 1 `
| select-string -pattern $pattern `
| foreach-object {
$match = $_.Matches[0];
$tmp_removeParen = $match.Groups['sdkDesc'] -replace '\(|\)|%s|\"',"."
[PSCustomObject] [ordered] @{
"sdkErr" = $($match.Groups['sdkErr'])
"sdkDesc" = $($tmp_removeParen)
"sdkOutErr" = $($match.Groups['sdkOutErr'])
"sdkSeverity" = ($match.Groups['sdkSeverity'] -split '_')[-1]
}
};
return $result
}#End of Process
}#End of Function
That gets all of the targeted contents plus the commented blocks, which I want to avoid. The c++ code that is being parsed looks like this:
case kRESULT_STATUS_SHORTAGE:
_stprintf(outDevStr, _T("2000 - (Shortage issue) - %s(Shortage)"), errorStr);
outError = HOP_SHORTAGE;
outSeverity = CCC_INFORMATION;
break;
// New Error codes(really old errors broken out with unique error codes) - not all have this line
//case kRESULT_STATUS_User_CoverOpenErr: //comment here
// _stprintf( outDevStr, _T("2900 - (Cover Open) - %s(Upper cover open.)"), errorStr);
// outError = HOP_COVER_OPEN;
// outSeverity = CCC_INFORMATION;
// break;
I tried changing the first part with the split to this, but it makes it return no results. I feel like if I just figure out how to not include a case block that is commented on the case line, it will fix everything.
$blocks = $parsedCaseMethod -split "(?=^[\s]+case (.*):)"; #didn't work - nothing in $result
Any help would be appreciated. Thanks! :)
This is with Powershell 5.1 and VS Code.