Wpf应用程序来计算特殊字符 [英] Wpf app to count special characters
本文介绍了Wpf应用程序来计算特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个WPF应用程序,它将通过 OpenFiledialoug 计算text.txt文件中的特殊字符并显示在文本框中...
问题是我我正面临一个例外:
i want to create a WPF app which will count the special characters from the text.txt file by OpenFiledialoug and display in textbox ...
The issue is i am facing an exception on the line :
var subRegex = Regex.Matches(file, item.ToString());
System.ArgumentException: 'parsing "\" - Illegal \ at end of pattern.'
我尝试过:
What I have tried:
private void OpenButon_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
if (openFile.ShowDialog() == true)
{
string filepath = openFile.FileName;
var regexString = @"[{(-_!@#$%^&*+<>,./\\?|':;~)}]";
var file = filepath;
var rex = new Regex(regexString, RegexOptions.IgnoreCase);
var results = rex.Matches(file);
foreach(var item in results)
{
var subRegex = Regex.Matches(file, item.ToString());
textbox.Text += "No of Character:" + item + " appearance: " + subRegex.Count;
}
}
}
推荐答案
%^& * +< ;>,。/ \\?|':;〜)}];
var file = filepath;
var rex = new Regex(regexString,RegexOptions.IgnoreCase) ;
var results = rex.Matches(file);
foreach(结果中的var项)
{
var subRegex = Regex.Matches(file,item.ToString()) ;
textbox.Text + =No of Character:+ item +appearance:+ subRegex.Count;
}
}
}
%^&*+<>,./\\?|':;~)}]"; var file = filepath; var rex = new Regex(regexString, RegexOptions.IgnoreCase); var results = rex.Matches(file); foreach(var item in results) { var subRegex = Regex.Matches(file, item.ToString()); textbox.Text += "No of Character:" + item + " appearance: " + subRegex.Count; } } }
这看起来很像昨天在不同的用户ID下发布的问题:计算C#WPF中文本文件中的特殊字符 [ ^ ]。
如果您是同一个人,请关闭重复的用户帐户。如果你只是朋友那么聚在一起互相帮助。
This looks suspiciously like the questions posted yesterday under a different userid: Count special characters from text file in C# WPF[^].
If you are the same person then please close the duplicate user account. If you are just friends then get together and help each other.
运行时是正确的(当然),因为 Regex.Matches(String,String) [ ^ ]期望正则表达式模式作为第二个参数,并且您没有传递模式。
请注意,您没有在文件中搜索特殊字符,而是在文件的路径中搜索它们。
我强烈建议你阅读.NET
正则表达式的教程。
The runtime is correct (of course), since the Regex.Matches(String, String)[^] expects a regex pattern as second parameter and you are not passing a pattern.
Note you are not searching for special characters inside the file, you are seraching for them inside the file's path.
I strongly advice you reading a tutorial on.NET
regular expressions.
这篇关于Wpf应用程序来计算特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文