【问题标题】:How to use (char)167 or § in wildcard. C# find and replace如何在通配符中使用 (char)167 或 §。 C# 查找和替换
【发布时间】:2019-11-12 13:12:56
【问题描述】:

我正在制作 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

实际上它应该像这样工作:@"&lt;(§) ([0-9])&gt;"

【问题讨论】:

  • 我在我的问题中添加了更多信息。如果我将使用通配符手动搜索(§) ([0-9]),它将起作用。但在 C# 中却没有。
  • 您确定手动搜索 实际上匹配 "§ 5" 吗?我问是因为匹配的 Word 在这里不起作用,
  • 我同意@yokki。当 § 是文档中的第一个字符时,该模式有效,否则无效。这种模式 - [ ^013]§ ([0-9])&gt; - 当字符前面有空格或段落标记时有效,但不会找到文档中的第一个字符,或者该字符是表格单元格中的第一个字符。这 - § ([0-9])&gt; - 适用于所有实例,但不区分字符之前是否有任何内容。我认为您需要最后使用它,然后确定紧接在找到的范围之前的字符是什么......
  • 谢谢你们。 § ([0-9])&gt; 是我要找的。现在看来很明显了。
  • 能否请您回复您自己的问题并将您的回复标记为答案?

标签: c# ms-word vsto wildcard office-interop


【解决方案1】:

谢谢你们。 § ([0-9])&gt; 是我要找的。现在看来很明显了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-09
    • 1970-01-01
    • 1970-01-01
    • 2021-01-22
    • 1970-01-01
    • 2020-10-29
    • 2023-04-04
    • 2018-07-04
    相关资源
    最近更新 更多