替换 powershell 中的“智能引号" [英] Replacing “smart quotes” in powershell

查看:44
本文介绍了替换 powershell 中的“智能引号"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现自己被一个简单的问题难住了.我试图从一堆文本文件中删除花哨的引用.我有以下脚本,我在其中尝试了多种不同的替换方法,但没有结果.

I'm finding myself somewhat stumpped on a simple problem. I'm trying to remove fancy quoting from a bunch of text files. I've the following script, where I'm trying a number of different replacement methods, but w/o results.

这是从github下载数据并尝试转换的示例.

Here's an example that downloads the data from github and attempts to convert.

$srcUrl="https://raw.github.com/gist/1129778/d4d899088ce7da19c12d822a711ab24e457c023f/gistfile1.txt"
$wc = New-Object net.WebClient
$wc.DownloadFile($srcUrl,"foo.txt")
$fancySingleQuotes = "[" + [string]::Join("",[char[]](0x2019, 0x2018)) + "]"

$c = Get-Content "foo.txt"
$c | % { `
        $_ = $_.Replace("’","'")
        $_ = $_.Replace("`"","`"")
        $_.Replace("`"","`"")       
    } `
    |  Set-Content "foo2.txt"

这有什么诀窍?

推荐答案

更新:修正了我的答案(manojlds 评论是正确的,$_ 事情是一个红鲱鱼).这是一个有效的版本,我已经更新它以包含您的测试代码:

UPDATE: Fixed my answer (manojlds comments were correct, the $_ thing was a red herring). Here's a version that works, and I've updated it to incorporate your testing code:

    $srcUrl="https://raw.github.com/gist/1129778/d4d899088ce7da19c12d822a711ab24e457c023f/gistfile1.txt"
    $wc = New-Object net.WebClient
    $wc.DownloadFile($srcUrl,"C:\Users\hartez\SO6968270\foo.txt")

    $fancySingleQuotes = "[\u2019\u2018]" 
    $fancyDoubleQuotes = "[\u201C\u201D]" 

    $c = Get-Content "foo.txt" -Encoding UTF8

    $c | % { `
        $_ = [regex]::Replace($_, $fancySingleQuotes, "'")   
        [regex]::Replace($_, $fancyDoubleQuotes, '"')     
    } `
    |  Set-Content "foo2.txt"

manojlds 版本不适合您的原因是您从 github 获取的文件的编码与正则表达式中的 Unicode 字符不兼容.以 UTF-8 格式读取它可以解决问题.

The reason that manojlds version wasn't working for you is that the encoding on the file you're getting from github wasn't compatible with the Unicode characters in the regex. Reading it in as UTF-8 fixes the problem.

这篇关于替换 powershell 中的“智能引号"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆