powershell - 在powershell中搜索文字连字符

标签 powershell

我正在使用 powershell 将多页 word 文档拆分为单页,然后删除我不需要的所有页面。问题是我正在复制文本并搜索模式,而连字符让我失望。这是相关代码。如果我删除连字符并只搜索“订单号”,它会起作用,但它会返回两页,而不是我想要的带有“订单号 -”模式的一页。我尝试了很多配置,转义连字符`-、--% 等但没有任何效果,我运行的所有内容包括连字符都只输出 8 个 DeleteMe 页面,而不是 1 个文档和 7 个 DeleteMe

    $Pattern = "Order Number -"
    $rngPg.End = $word.Selection.Start
    $rngPg.Copy()
    #Get Name 
    $regex = [Regex]::Match($rngPg.Text, $Pattern) 
    if($regex.Success)
    {
    $id = "Document" + $i 
    }
    else
    {
    $id = "DeleteMe_" + $i 
    }

最佳答案

将您的模式更改为:

$Pattern = 'Order Number [\u2010-\u2015-]'

解释:
Word 有一个 AutoFormat feature喜欢自动将连字符转换为破折号 - 这里可能就是这种情况。
英语语言办公室将连字符转换为具有代码点值的 unicode 字符 0x2013 ,但它可能因区域设置和安装的语言包而异,因此来自 0x2010 的字符集至 0x2015 + -

关于powershell - 在powershell中搜索文字连字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38487021/

相关文章:

有关排序和管道的 PowerShell 问题

sql - 使用条件从 powershell 脚本运行脚本

powershell - PowerShell New-ADUser循环失败

powershell - 从文件夹中选择最新文件,然后使用Powershell脚本将其复制到另一个文件

powershell - ADAL 和 Azure 自动化

powershell - 如何使用 PowerShell 从 OneNote 文档中提取标题

powershell - 在 powershell 中添加第二行

powershell - new-itemproperty 和 set-itemproperty 有什么区别?

google-chrome - 使用注册表项修复在 Chrome 中启用 Silverlight 插件 (NPAPI)

powershell - 远程执行脚本的脚本位置?