2

PowerShell で正規表現の作成に取り組んでいます。これは、テーブル名と列名にスペースが含まれないように SQL を変換する VS2012 からのビルド後のイベントの開始です。これが機能したら、既に必要なスクリプトを変更して、ファイルの内容を正規表現文字列に置き換えることができます。私はこのチュートリアルで作業してきましたが、スペースが開き角括弧と閉じ角括弧の間にある場合、スペース (\s) をアンダースコアに置き換えることができないようです。

SQL を変換する方法の例を次に示します。

変換:

select * from [Existing product] where [Existing product].[Desc value] = 26

に:

select * from [Existing_product] where [Existing_product].[Desc_value] = 26

このスクリプトを powershell ISE で実行すると、次のようになります。

#Example of PowerShell Regex Replace
$newline = '
'
$strText = 'select * from [Existing product] where [Existing product].[Desc value] = 26'
$Pattern = '(?<=\[)(\s(?<=\s))(?=\])'
$New = "_"
$newline
'SourceText: '
$strText
$newline
$strReplace = [regex]::replace($strText, $pattern, "$New")
"We will now replace $Pattern with $New :" 
$newline
$strReplace

私はこの結果を得ます:

PS C:\> C:\REGEX.ps1

SourceText: 
select * from [Existing product] where [Existing product].[Description value] = 26


We will now replace (?<=\[)(\s(?<=\s))(?=\]) with _ :


select * from [Existing product] where [Existing product].[Description value] = 26

上記のスペースがアンダースコアに置き換えられた文字列が表示されることを願っています。

私の正規表現は現在(?<=\[)(\s(?<=\s))(?=\])ですが、角括弧がすぐ隣にあるスペースのみを選択しているようです。上記の正規表現には何が欠けていますか?

ご不明な点がございましたら、お気軽にお問い合わせください。ご協力いただきありがとうございます。

4

2 に答える 2