如何使用(char)167或§;用通配符。C#查找并更换
我正在制作VSTO应用程序。我正在使用FindAndReplace函数。用于查找模式。但是我不能用§。这不管用 我试过了如何使用(char)167或§;用通配符。C#查找并更换,c#,ms-word,vsto,wildcard,office-interop,C#,Ms Word,Vsto,Wildcard,Office Interop,我正在制作VSTO应用程序。我正在使用FindAndReplace函数。用于查找模式。但是我不能用§。这不管用 我试过了 char K = (char)167; string[] nimed = new string[] {$@"<({K}) ([0-9])>", , @"<([1-5])[-^~]([0-9]{2})[-^~]([0-9]{1;6})>"} chark=(char)167; 字符串[]nimed=新字符串[]{$@“@”} 它找到第二种模式,但不是第
char K = (char)167;
string[] nimed = new string[] {$@"<({K}) ([0-9])>", , @"<([1-5])[-^~]([0-9]{2})[-^~]([0-9]{1;6})>"}
chark=(char)167;
字符串[]nimed=新字符串[]{$@“@”}
它找到第二种模式,但不是第一种。但如果我想改变其他的东西,那它就行了
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.Range查找和替换(Word.Range rngToSearch、object findText、object replaceWithText)
{
bool found=false;//选项
objectmatchcase=false;
对象匹配wholeword=true;
对象匹配通配符=true;
对象匹配Soundslike=false;
对象matchAllWordForms=false;
对象转发=假;
对象格式=false;
object matchKashida=false;//用于搜索和包装文本的函数。它还可以替换包装文本。
对象匹配DicalCritics=false;
对象匹配alefhamza=false;
对象匹配控件=false;
对象只读=假;
对象可见=真实;
对象替换=false;
对象包裹=1;
//执行查找和替换
Find=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);
如果(!找到)
{
rngToSearch=null;
}
返回rngToSearch;
}
私有无效按钮3\u单击(对象发送者,RibbonControlEventArgs e)
{
字符K=(字符)167;
字符串[]nimed=新字符串[]{@“@”@“@”@“$@”};
Word.Document doc=Globals.ThisAddIn.Application.ActiveDocument;
File.AppendAllText(@“C:\install\CSharp\tulemus.txt”,$”字符为:{K}“+Environment.NewLine);//返回§所以K是正确的字符
Word.Range rng=单据内容;
foreach(nime中的字符串nimi)
{
如果(findandplace(rng,$“{nimi},”“)!=null){
while(findandplace(rng,$“{nimi},”“)!=null)
{
Range f=findandplace(rng,$“{nimi}”,”);
FindAndReplace2(rng,$“{nimi}”,f.Text.Replace((char)45,(char)160.Replace((char)32,(char)30));
}}
}
}
没有错误消息。它只是一个搜索。但是,如果我在Word中手动尝试,它就会工作。如何使§在C#通配符中可搜索
我尝试搜索的示例文本是:
§5
实际上它应该是这样工作的:
@“
谢谢大家<代码>§([0-9])>是我要找的。现在看来很明显。我在问题中添加了更多信息。如果我将使用通配符手动搜索(§)([0-9])
,它将起作用。但在C#中,它不是。你绝对确定手动搜索实际上与“§5”匹配吗?我这样问是因为这个匹配词在这里不起作用,我也不同意@yokki。当§是文档中的第一个字符时,该模式起作用,但在其他情况下不起作用。此模式-[^013]§([0-9])>
在字符前面有空格或段落标记时起作用,但在文档中找不到第一个字符,也找不到表格单元格中的第一个字符。此-§([0-9])>
适用于所有实例,但不区分字符前面是否有任何内容。我想你们需要使用最后一个,然后确定紧跟在找到的范围之前的字符是什么…谢谢你们<代码>§([0-9])>是我要找的。现在看起来很明显。你能回答你自己的问题并把你的回答标记为答案吗?