我正在寻找一个正则表达式来匹配 PowerShell 中的字符串 @'...'@并且@"..."@是这里的字符串
规则:
@'或@")'@或'@),它总是在行首,但是可以有更多的文本@' .. '@可能包括内部@" "@但在这种情况下,外部将匹配。例子
$MyString = @"
hello
@'
'@
bye
"@
Run Code Online (Sandbox Code Playgroud)
$MyString = @'
hello
@"
"@
bye
'@
Run Code Online (Sandbox Code Playgroud)
$page = [XML] @"
<command:command xmlns:maml="http://schemas.microsoft.com/maml/2004/10"
xmlns:command="http://schemas.microsoft.com/maml/dev/command/2004/10"
xmlns:dev="http://schemas.microsoft.com/maml/dev/2004/10">
<command:details>
<command:name>
Format-Table
</command:name>
<maml:description>
<maml:para>Formats the output as a table.</maml:para>
</maml:description>
<command:verb>format</command:verb>
<command:noun>table</command:noun>
<dev:version></dev:version>
</command:details>
...
</command:command>
"@
Run Code Online (Sandbox Code Playgroud)
我很感激任何帮助。我正在尝试解决内联问题,以便更好地为privacy.sexy 提供PowerShell 模板支持,因此您的帮助将在社区中扩展到更多。
你最好使用 PowerShell 的语言解析器, System.Management.Automation.Language.Parser与基于正则表达式的解决方案相比。
我假设你总是对外部感兴趣here-string,而不是恰好嵌套在另一个中的字符串。
假设文件file.ps1具有以下逐字内容:
$MyString1 = @'
hello
@"
"@
bye
'@
$MyString2 = @"
hello
@'
'@
bye
"@
Run Code Online (Sandbox Code Playgroud)
以下命令:
$ast = [System.Management.Automation.Language.Parser]::ParseInput(
(Get-Content -Raw file.ps1),
[ref]$null,
[ref]$null
)
$ast.FindAll(
{
$args[0] -is [System.Management.Automation.Language.StringConstantExpressionAst] -and
$args[0].StringConstantType -in 'SingleQuotedHereString', 'DoubleQuotedHereString'
},
$true
) | Format-Table StringConstantType, Value -Wrap
Run Code Online (Sandbox Code Playgroud)
输出:
StringConstantType Value
------------------ -----
SingleQuotedHereString hello
@"
"@
bye
DoubleQuotedHereString hello
@'
'@
bye
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
51 次 |
| 最近记录: |