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
}
Change your pattern to:
$Pattern = 'Order Number [\u2010-\u2015-]'
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 + -
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With