2016-04-12 7 views
0

内の文字列内の変数を探す:私はこのようなサンプルコードてきたのPowerShell

[string] $Text = "This is a string text with some $($variable.Option) and $otherOption and that's it". 

を今私が知りたいのですがどのようなそれは別に、標準の文字列と変数にその$Textを分割することは可能でしょうか?この$Text変数をメソッドに渡すと、別に$($variable.Option)を抽出できますか?

私はそれが長いショットだと知っていますが、割り当て時にすぐに処理されていない可能性がありますか?

最終的な目標はカラフルPowerShellを作るためbetter version of method I wroteを作成することです:

function Write-Color([String[]]$Text, [ConsoleColor[]]$Color = "White", [int]$StartTab = 0, [int] $LinesBefore = 0,[int] $LinesAfter = 0) { 
    $DefaultColor = $Color[0] 
    if ($LinesBefore -ne 0) { for ($i = 0; $i -lt $LinesBefore; $i++) { Write-Host "`n" -NoNewline } } # Add empty line before 
    if ($StartTab -ne 0) { for ($i = 0; $i -lt $StartTab; $i++) { Write-Host "`t" -NoNewLine } } # Add TABS before text 
    if ($Color.Count -ge $Text.Count) { 
     for ($i = 0; $i -lt $Text.Length; $i++) { Write-Host $Text[$i] -ForegroundColor $Color[$i] -NoNewLine } 
    } else { 
     for ($i = 0; $i -lt $Color.Length ; $i++) { Write-Host $Text[$i] -ForegroundColor $Color[$i] -NoNewLine } 
     for ($i = $Color.Length; $i -lt $Text.Length; $i++) { Write-Host $Text[$i] -ForegroundColor $DefaultColor -NoNewLine } 
    } 
    Write-Host 
    if ($LinesAfter -ne 0) { for ($i = 0; $i -lt $LinesAfter; $i++) { Write-Host "`n" } } # Add empty line after 
} 

通常、私は

write-color -Text "[View][$($singleView.Title)]", 
        "[Row Limit: $($singleView.RowLimit)]", 
        "[Paged: $($singleView.Paged)]", 
        "[Default View: $($singleView.DefaultView)]", 
        "[Style ID: $($singleView.StyleID)]" -Color Yellow, Green, Red, Gray, Green 

ような何かをすることによって色を割り当てることができます。しかし、これは「私は、全体の色を得ることを意味ライン"。

write-color -Text "[View: ", "$($singleView.Title)", "]", 
        "[Row Limit: ", "$($singleView.RowLimit)", "]" ` 
      -Color Yellow, Green, Yellow, Yellow, Green, Yellow 

それは悪くないです..しかし、これはAで行うことができる場合、私はちょうど考えた:私は1つの色と第2の色にある変数で、通常のテキストの色を取得したい場合、私はこのような何かをしなければならないでしょう単純なテキストが1つの色で、変数が2番目の場合はより良い方法です。私がさらに進んで、$ trueをGreenとFalseにしてRedで書いておきたいと思っていれば、何か解析が必要になります。

+1

私はそうは思わない。私が知っている限り、文字列は処理され、すぐに解析され、すべての埋め込み変数が解決され、結果の文字列は '$ Text'に格納されます。 '$ Text'はそれを定義するための変数についての知識がありません。 –

+1

具体的に何を実装したいのですか?これは私にとって非常に厄介です。 –

+0

これはXY問題の場合、可能であればどのようにこの情報を使用するかの具体的な例があります(これはBaconビットが言ったことが原因ではありません)。オハイオ州.......あなたは遅れた文字列の拡張を要求しています..... – Matt

答えて

1

変数は二重引用符で囲みます。それが終わると、話すべき歴史はありません。

ここには2つのオプションのいずれかがあります。書式演算子を使用すると、変数のプレースホルダを文字列で送信できます。

は、一回の使用でdelay the string expansionあなたは本当に私はあなたが求めている何を考えて欲しかった場合のことができます。Using the format operator

# Create a formatted string with variable placeholders 
$Text = "This is a string text with some {0} and {1} and that's it" 

# Pass that string into a function and put in the variables 
$stringParameterinFunction -f $variable.Option, $otherOption 

文字列展開

にフォーマットオペレータ

ss64を使用します元の文字列の引用符。文字列内の一重引用符がエスケープされていることに注意してください。

$Text = 'This is a string text with some $($variable.Option) and $otherOption and that''s it.' 

# Pass that string into a function and put in the variables 
$ExecutionContext.InvokeCommand.ExpandString($stringParameterinFunction) 
関連する問題