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.
CodePudding user response:
The simplest approach is probably to eliminate all comment lines in a first step, before splitting:
$blocks = $parsedCaseMethod -replace '(?m)^\s*//.*' -split '(?=case (.*):)'
Note:
To keep the regex simple, the above effectively replaces the comment lines with empty lines (it does, however, remove empty and all-whitespace lines preceding a comment line). If you want to avoid that, use
-replace '(?m)^\s*//.*(?:\r?\n)?'
- For an explanation of the regex and the ability to experiment with it, see this regex101.com page.
The assumption is that your C code doesn't contain multi-line comments (
/* ... */
), and no//
-prefixed lines inside C 11 raw string literals.