Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In this coding I found the emdash endash and underscore , between two words, that two words stored in list, but my replace not worked any one have better solution.

C#
public void EnDashToHyphen(Office.IRibbonControl control)
        {
         
            Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
            MessageBox.Show("tested");

            List<string> AcidList = new List<string>()
            {
                 {"One"},{"Two"},{"The"},{"Four"},{"Five}        
       };
            foreach(Word.Range story in doc.StoryRanges)
            {
                foreach(Word.Paragraph para in story.Paragraphs)
                {
                    //Word.Range temp;
                    foreach(Word.Range w in para.Range.Words)
                    {
                        Word.Range d = w.Duplicate;
                       
                        if (Regex.IsMatch(w.Text, "[\u2013\u2014\u005F]", RegexOptions.Multiline))
                        {
                            if (AcidList.Contains(w.Previous(Word.WdUnits.wdWord,1).Text) && AcidList.Contains(w.Previous(Word.WdUnits.wdWord,1).Text))
                            {
                                Regex.Replace(d.ToString(), "^[\u2013\u2014\u005F]$", "^HYPHEN$");
                                //MessageBox.Show(w.Text);
                            }
                        }
                    }
                }
            }
        }
Posted
Updated 27-Feb-18 1:53am

1 solution

That Regex isn't "emdash endash and underscore , between two words" - it's "a line (or whole string) containing one single character, that must be one of emdash, endash, or underscore"

I'd sugget that you try testing the regex first: Get a copy of Expresso [^] - it's free, and it examines and generates Regular expressions.
And yes, it'll let you test Replace.
 
Share this answer
 
Comments
smksamy 16-Dec-14 7:52am    
I have some knowledge in regex, but I try regex with range value, anyway thanks for your suggestion

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900