【发布时间】:2014-06-24 00:39:51
【问题描述】:
我在计算所有 powershell 项目中的代码行数时遇到了一些麻烦。 我想忽略我的计数中的评论部分,不幸的是我对正则表达式不太擅长。 所以我想要实现的是排除所有“函数中的概要帮助代码”
<#
.SYNOPSIS
#>
还有我自己的评论区
<#
Get-ADUser -Identity ThisUserDoesNotExist
ThisCodeIsCommentedOut
#>
到目前为止我所拥有的是
Get-Content Script.ps1 | ?{$_ -ne "" -and $_ -notlike "#*"}
【问题讨论】:
-
我想我找到了解决方法: function Count-CommentBlock { param ( $ScriptText ) $boolInsideComment = $false $lineCounter = 0 $lineTotal = 0 $ScriptText = $ScriptText.Trim() foreach ( $Line in $ScriptText) { if ($Line -like "*") { $boolInsideComment = $false $lineCounter++ $lineTotal += $lineCounter } if ($boolInsideComment) { $lineCounter++ } } return $lineTotal } 这应该有效吗?
-
为什么不使用我的解决方案?
标签: regex powershell powershell-3.0 string-matching