如何在通配符中使用 (char)167 或 §。 C# 查找和替换

How to use (char)167 or § in wildcard. C# find and replace

我正在制作 VSTO 应用。我正在使用 FindAndReplace 函数。用于寻找模式。但我不能使用§。它没有用。

我试过了

char K = (char)167;
string[] nimed = new string[] {$@"<({K}) ([0-9])>", , @"<([1-5])[-^~]([0-9]{2})[-^~]([0-9]{1;6})>"}

它找到了第二个模式,但没有找到第一个。但是,如果我将 § 更改为其他内容,则它会起作用。

 Word.Range FindAndReplace(Word.Range rngToSearch, object findText, object replaceWithText)
        {
            bool found = false; //options
            object matchCase = false;
            object matchWholeWord = true;
            object matchWildCards = true;
            object matchSoundsLike = false;
            object matchAllWordForms = false;
            object forward = false;
            object format = false;
            object matchKashida = false;                      //Function for searching and wrapping the text. It can also replace the wraped text.
            object matchDiacritics = false;
            object matchAlefHamza = false;
            object matchControl = false;
            object read_only = false;
            object visible = true;
            object replace = false;
            object wrap = 1;
            //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;
        }
        private void Button3_Click(object sender, RibbonControlEventArgs e)
        {


            char K = (char)167;
            string[] nimed = new string[] {@"<(3)[-]([1-4])[-^~]([1-4])[-^~]([0-9]{1;3})?([0-9]{2})>", @"<([1-5])[-^~]([0-9]{2})[-^~]([0-9]{1;6})>", @"<([1-5])[-]([0-9]{2})[-^~]([0-9]{1;6})/([0-9]{1;4})>", $@"<({K}) ([0-9])>" };
            Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
File.AppendAllText(@"C:\install\CSharp\tulemus.txt", $"Char is: {K}" + Environment.NewLine); //returns § so K is right char
            Word.Range rng = doc.Content;
            foreach (string nimi in nimed)
            {
                if (FindAndReplace(rng, $"{ nimi}", "") != null) { 
                while (FindAndReplace(rng, $"{ nimi}", "") != null)
                {
                    Word.Range f = FindAndReplace(rng, $"{ nimi}", "");

                    FindAndReplace2(rng, $"{ nimi}", f.Text.Replace((char)45, (char)160). Replace((char)32, (char)30));



                }}

            }

        }

没有错误信息。它只是不搜索。但是如果我在 Word 中手动尝试它,那么它就可以工作。如何在 C# 通配符中使 § 可搜索。

我要搜索的示例文本是:

§ 5

实际上它应该是这样工作的:@"<(§) ([0-9])>"

谢谢大家。 § ([0-9])> 是我要找的。现在看来很明显了。