Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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_Powershell 3.0_String Matching - Fatal编程技术网

Regex Powershell代码行数

Regex Powershell代码行数,regex,powershell,powershell-3.0,string-matching,Regex,Powershell,Powershell 3.0,String Matching,我在计算我所有powershell项目中的代码行时遇到了一些问题。 我想忽略我计数中的注释部分,不幸的是,我对正则表达式不是很在行。 因此,我想要实现的是排除所有“函数中的大纲帮助代码” 如果您在v3.0中,我建议使用以下脚本: 此处修改的相关部分仅用于计算脚本文件的代码行数: $file = ".\My_Script_File.ps1" $fileContentsArray = Get-Content -Path $file if ($fileContentsArray) {

我在计算我所有powershell项目中的代码行时遇到了一些问题。 我想忽略我计数中的注释部分,不幸的是,我对正则表达式不是很在行。 因此,我想要实现的是排除所有“函数中的大纲帮助代码”


如果您在v3.0中,我建议使用以下脚本:

此处修改的相关部分仅用于计算脚本文件的代码行数:

$file = ".\My_Script_File.ps1"

$fileContentsArray  = Get-Content -Path $file

if ($fileContentsArray)
    {
        $codeLines          = $null
        $tokenAst           = $null
        $parseErrorsAst     = $null
        # Use the PowerShell 3 file parser to create the scriptblock AST, tokens and error collections
        $scriptBlockAst     = [System.Management.Automation.Language.Parser]::ParseFile($file, [ref]$tokenAst, [ref]$parseErrorsAst)
        # Calculate the 'lines of code': any line not containing comment or commentblock and not an empty or whitespace line.
        # Remove comment tokens from the tokenAst, remove all double newlines and count all the newlines (minus 1)
        $prevTokenIsNewline = $false
        $codeLines      = @($tokenAst | select -ExpandProperty Kind |  where { $_ -ne "comment" } | where {
                                if ($_ -ne "NewLine" -or (!$prevTokenIsNewline))
                                {
                                    $_
                                }
                            $prevTokenIsNewline = ($_ -eq "NewLine")
                            } | where { $_ -eq "NewLine" }).Length-1
    $codeLines
}

我想我找到了解决方法:函数Count CommentBlock{param($ScriptText)$boolInsideComment=$false$lineCounter=0$lineTotal=0$ScriptText=$ScriptText.Trim()foreach($ScriptText中的行){if($Line-like“*”){$boolInsideComment=$false$lineCounter++$lineTotal++=$lineCounter}如果($boolInsideComment){$lineCounter++}返回$lineTotal}这应该行得通?为什么不使用我的解决方案?
<#
Get-ADUser -Identity ThisUserDoesNotExist
ThisCodeIsCommentedOut
#>
Get-Content Script.ps1 | ?{$_ -ne "" -and $_ -notlike "#*"}
$file = ".\My_Script_File.ps1"

$fileContentsArray  = Get-Content -Path $file

if ($fileContentsArray)
    {
        $codeLines          = $null
        $tokenAst           = $null
        $parseErrorsAst     = $null
        # Use the PowerShell 3 file parser to create the scriptblock AST, tokens and error collections
        $scriptBlockAst     = [System.Management.Automation.Language.Parser]::ParseFile($file, [ref]$tokenAst, [ref]$parseErrorsAst)
        # Calculate the 'lines of code': any line not containing comment or commentblock and not an empty or whitespace line.
        # Remove comment tokens from the tokenAst, remove all double newlines and count all the newlines (minus 1)
        $prevTokenIsNewline = $false
        $codeLines      = @($tokenAst | select -ExpandProperty Kind |  where { $_ -ne "comment" } | where {
                                if ($_ -ne "NewLine" -or (!$prevTokenIsNewline))
                                {
                                    $_
                                }
                            $prevTokenIsNewline = ($_ -eq "NewLine")
                            } | where { $_ -eq "NewLine" }).Length-1
    $codeLines
}