NON-BREAKING HYPHEN in Word interop C# Range

284 Views Asked by At

Please help. I have a code. The code searches @"<(3)[-^~]([1-4])[-^~]([1-4])[-^~]([0-9]{1;3})?([0-9]{2})>" //(strings like 3-2-1-123-15 ) and puts the hyperlinks on it. But imagine, if the

3-2-1-123-15

has NON-BREAKING HYPHEN between numbers. If I put range to text (string s = rng.Text;)(I need it because I am using the same string in my hyperlink: rngFound.Range.Hyperlinks.Add(rngFound.Range, hyperlink + rngFound.Text)); then I will have:

32112315

situation. But how can I have string with "-". I mean how can I replace NON-BREAKING HYPHEN with "-". Do I need to replace it in the word text?(It is not very good). And if I do then how can I do that using function:

Word.Selection FindAndReplace2(Word.Selection rngToSearch, object findText, object replaceWithText) //Find function
        {
            bool found = false;
            //options
            object matchCase = false;
            object matchWholeWord = true;
            object matchWildCards = true;
            object matchSoundsLike = false;
            object matchAllWordForms = false;
            object forward = true;
            object format = false;
            object matchKashida = false;
            object matchDiacritics = false;
            object matchAlefHamza = false;
            object matchControl = false;
            object read_only = false;
            object visible = true;
            object replace = true;
            object wrap = Word.WdFindWrap.wdFindStop; ;

            //execute find and replace
            found = rngToSearch.Find.Execute(ref findText, ref matchCase, ref matchWholeWord,
                ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref replaceWithText, ref replace,
                ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl);
            if (!found)
            {
                rngToSearch = null;
            }

            return rngToSearch; 
1

There are 1 best solutions below

3
On BEST ANSWER

The symbol for a non-breaking hyphen is ANSI 31: ¬. This can be included in the search string, as illustrated below

(<(3)[-^~¬]([1-4])[-^~¬]([1-4])[-^~¬]([0-9]{1;3})?([0-9]{2})>

My tests found both the original string in the question, as well as one where ¬ replaced the -.

Once Find is successful, the Range object contains the text that was searched. It should be no problem to assign that to a string and perform Replace on the string, only (so that it doesn't affect the text in the document, only the string used to create the hyperlink). Something like this (untested):

string foundNr = rngToSearch.Text;
string hyperlinkNr = foundNr.Replace("¬", "-");