Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching for a literal hyphen in powershell

Tags:

powershell

I am using powershell to split a multipage word document into single pages and then deleting all the pages I don't need. The problem is I am copying the text and searching for a pattern and the hyphen is throwing me off. This is the relevant code. If I remove the hyphen and just search for "Order Number", it works but it comes back with two pages instead of just the one I want with the pattern "Order Number -". I have tried a lot of configurations, escaping the hypen `-, --%, etc but nothing works, everything I run that includes the hyphen just outputs 8 DeleteMe pages, instead of 1 Docuemnt and 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 
    }
like image 925
Nolemonkey Avatar asked Oct 20 '25 13:10

Nolemonkey


1 Answers

Change your pattern to:

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

Explanation:

Word has an AutoFormat feature that loves to automagically turn hyphens into dashes - that may be the case here.

English language Office turns hyphens into unicode characters with the codepoint value 0x2013, but it may vary depending on locale and installed language packs, thus the character set from 0x2010 to 0x2015 + -

like image 58
Mathias R. Jessen Avatar answered Oct 23 '25 03:10

Mathias R. Jessen