Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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 似乎无法使正则表达式匹配_Regex_Powershell - Fatal编程技术网

Regex 似乎无法使正则表达式匹配

Regex 似乎无法使正则表达式匹配,regex,powershell,Regex,Powershell,我正在尝试使用PowerShell从PowerShell脚本中提取Get-Help注释标题。我正在读取的文件如下所示: <# .SYNOPSIS Synopsis goes here. It could span multiple lines. Like this. .DESCRIPTION A description. It could also span multiple lines.

我正在尝试使用PowerShell从PowerShell脚本中提取Get-Help注释标题。我正在读取的文件如下所示:

<#
    .SYNOPSIS
        Synopsis goes here.
        It could span multiple lines.
        Like this.

    .DESCRIPTION 
        A description.
        It could also span multiple lines.

    .PARAMETER MyParam
        Purpose of MyParam

    .PARAMETER MySecondParam
        Purpose of MySecondParam.
        Notice that this section also starts with '.PARAMETER'.
        This one should not be captured.

    ...and many many more lines like this...
#>

# Rest of the script...
以下是我尝试过的:

$script=Get Content-Path C:\Path\to\the\script.ps1-Raw $pattern='\.DESCRIPTION.*?\.PARAMETER' $description=$script |选择字符串-模式$Pattern 写入主机$description 当我运行该命令时,$description是空的。如果我将$pattern更改为。*,我将获得文件的全部内容,正如预期的那样;所以我的正则表达式模式肯定有问题,但我似乎无法理解

有什么想法吗?

Select-String cmdlet适用于整个字符串,您只给了它一个字符串。[咧嘴笑]

因此,我没有与之抗争,而是选择了-match接线员。下面假定您已将整个文件作为一个带-Raw的多行字符串加载到$InStuff中

ms是两个正则表达式标志-多行和单行

$InStuff -match '(?ms)(DESCRIPTION.*?)\.PARAMETER'

$Matches.1
输出

DESCRIPTION 
        A description.
        It could also span multiple lines.
请注意,末尾有一个空行。你可能会想把它删掉

用以下词语:

不要使用正则表达式在PowerShell中解析PowerShell代码

改用PowerShell解析器

那么,让我们使用PowerShell来解析PowerShell:

$ScriptFile = "C:\path\to\the\script.ps1"

$ScriptAST = [System.Management.Automation.Language.Parser]::ParseFile($ScriptFile, [ref]$null, [ref]$null)

$ScriptAST.GetHelpContent().Description
我们使用[System.Management.Automation.Language.Parser]::ParseFile来解析我们的文件并输出一个抽象语法树

一旦我们有了抽象语法树,我们就可以使用GetHelpContent方法,就像Get Help用来获取解析的帮助内容一样


因为我们只对描述部分感兴趣,所以只需使用.GetHelpContent.Description直接访问它即可!太漂亮了![咧嘴笑]@Lee_Dailey我以为你会喜欢的-我现在正在玩AST的东西。我完全忘记了有这样一件事。。。我不知道你能做到,酷!我将尝试这种方法,谢谢!我试图从我写的脚本中提取它,它不是一个已安装的模块。但是很酷的把戏!获取帮助。\script.ps1.description哦,哇,刚刚测试过,它成功了。更简单,谢谢!不管我最后的评论如何,这是有效的,当我测试这个解决方案时,我不知何故将它指向了一个没有描述头的脚本。向上投票,谢谢你的帮助!另外,当你说可以处理整个字符串时,你给了它一个字符串,你的意思是它可以处理一组字符串,而我只给了它一个字符串吗?只是想让我明白你的意思。@LewsTherin-不客气![grin]////当您使用Get Content-Raw时,会得到一个多行字符串。试着在上面输入一个.GetType。如果使用不带-Raw参数的Get Content,则会得到一个单行字符串数组。选择字符串的工作方式是选择整个字符串。。。如果你给它一根线。。。然后它最多会找到一个字符串。[咧嘴笑]
$ScriptFile = "C:\path\to\the\script.ps1"

$ScriptAST = [System.Management.Automation.Language.Parser]::ParseFile($ScriptFile, [ref]$null, [ref]$null)

$ScriptAST.GetHelpContent().Description
(get-help get-date).description

The `Get-Date` cmdlet gets a DateTime object that represents the current date 
or a date that you specify. It can format the date and time in several Windows 
and UNIX formats. You can use `Get-Date` to generate a date or time character 
string, and then send the string to other cmdlets or programs.

(get-help .\script.ps1).description