简体   繁体   中英

Does there exists a PowerShell Escape function for Special Characters

As with in the self-answer, I like to be able to remove control characters from text to get a single line string.
Does there exists a PowerShell Escape function for Special PowerShell Characters ?
(Similar to the Regex.Escape(String) Method for escaping special regex characters)
Or is there a smarter way to do this than the self-answer?
(I am less concerned with the Unicode escape sequence: `u{x} )

Using a regular expression callback :

$Backtick = @{}
foreach ($Char in '0abefnrtv`'.ToCharArray()) {
    $Backtick[$ExecutionContext.InvokeCommand.ExpandString("``$Char")] = "``$Char"
}
$SpecialCharacters = '[' + (-Join $Backtick.get_Keys()) + ']'
function Escape($Text) {
    [regex]::Replace($Text, $SpecialCharacters, { param($Match) $Backtick[$Match.Value] })
}

Escape 'Test
123'

Test`n123

Not as good as your code, but here's an alternative way:

function Escape([string]$Text) {
    if (![string]::IsNullOrWhiteSpace($Text)) { 
        $special = 0,7,8,9,10,11,12,13,27,96 | ForEach-Object { [char]$_ }
        $escaped = '`0', '`a', '`b', '`t', '`n', '`v', '`f', '`r', '`e', '`'
        0..($special.Count -1) | ForEach-Object { $Text = $Text -replace $special[$_], $escaped[$_] }
    }
    $Text
}
Escape 'Test
123'

# --> Test`r`n123

To offer a prepackaged solution based on the Debug-String function , available as an MIT-licensed Gist :

Assuming you have looked at the linked code to ensure that it is safe (which I can personally assure you of, but you should always check), you can install it directly as follows:

irm https://gist.github.com/mklement0/7f2f1e13ac9c2afaf0a0906d08b392d1/raw/Debug-String.ps1 | iex

The primary purpose of Debug-String is to visualize (hidden) control characters and spaces in strings (see this answer ), but it also offers the -AsSourceCode and -SingleLine switches, which produce the desired single-line, control-characters-as-escape-sequences string representations (albeit with the enclosing " chars., but you can easily trim them with .Trim('"') .

Additionally, you may use -UnicodeEscapes in order to represent non-ASCII-range characters (eg é ) by their Unicode escape sequences (eg `u{e9} ), though note that such escapes are only recognized in PowerShell (Core) v6+ .

Debug-String -AsSourceCode -SingleLine -UnicodeEscapes 'Tést`1
123'

The above yields (verbatim, including the " chars.), which can be copied and pasted into PowerShell code to expand to the original string:

"T`u{e9}st``1`n123"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM