Regex power shell如何将\n视为文字regunescape

Regex power shell如何将\n视为文字regunescape,regex,powershell,newline,Regex,Powershell,Newline,我正在用power shell解压一些字符串,遇到了一个小问题。如何在将字符串视为文字时取消对其进行扫描 乙二醇 结果: newline newline M E 预期结果 \n\nM\ne 由于您不能从Unescape方法中排除任何内容,我只想让它取消所有内容并将文字换行放回: [regex]::Unescape($str).Replace("`n", '\n') 由于您不能从Unescape方法中排除任何内容,我只想让它取消所有内容并将文字换行放回: [regex]::Unescape(

我正在用power shell解压一些字符串,遇到了一个小问题。如何在将字符串视为文字时取消对其进行扫描

乙二醇

结果:

newline
newline
M
E
预期结果

\n\nM\ne

由于您不能从
Unescape
方法中排除任何内容,我只想让它取消所有内容并将文字换行放回:

[regex]::Unescape($str).Replace("`n", '\n')

由于您不能从
Unescape
方法中排除任何内容,我只想让它取消所有内容并将文字换行放回:

[regex]::Unescape($str).Replace("`n", '\n')
虽然我认为这是最简单的方法,但是可以使用helper函数来替换字符串中的unicode文本

比如:

function Convert-UnicodeLiterals {
    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Mandatory = $true, Position = 0)]
        [string]$Text    
    )
    $regex = [regex] '(?i)\\u[0-9a-f]{4}'
    $match = $regex.Match($Text)
    while ($match.Success) {
        $codePoint = [int]($match.Value -replace '^\\u', '0x')
        $Text = $Text.Replace($match.Value, [string][char]::ConvertFromUtf32($codePoint))
        $match = $match.NextMatch()
    } 

    $Text
}

$str = "\n\n\u041c\n\u0435" | Convert-UnicodeLiterals
结果:

\n\nМ\nе
虽然我认为这是最简单的方法,但是可以使用helper函数来替换字符串中的unicode文本

比如:

function Convert-UnicodeLiterals {
    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Mandatory = $true, Position = 0)]
        [string]$Text    
    )
    $regex = [regex] '(?i)\\u[0-9a-f]{4}'
    $match = $regex.Match($Text)
    while ($match.Success) {
        $codePoint = [int]($match.Value -replace '^\\u', '0x')
        $Text = $Text.Replace($match.Value, [string][char]::ConvertFromUtf32($codePoint))
        $match = $match.NextMatch()
    } 

    $Text
}

$str = "\n\n\u041c\n\u0435" | Convert-UnicodeLiterals
结果:

\n\nМ\nе