Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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;
               }
           }
       }
Posted
Updated 31-Jan-18 22:44pm

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.
 
Share this answer
 
Comments
Member 13655335 1-Feb-18 4:09am    
yes i pick the code from that post but i don't know him .... but please can u tell me where am i going wrong?
Richard MacCutchan 1-Feb-18 4:30am    
You have some unescaped special characters in your string, see Regular Expression Language - Quick Reference | Microsoft Docs[^].

You also do not need the double backslash if you have the @ prefix on your string literal.
OriginalGriff 1-Feb-18 4:53am    
Yes you do: backslash is also an escape character for the regex processor itself.
Richard MacCutchan 1-Feb-18 4:58am    
:thumbsup:

Is that really the best emoji for "thanks"?
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.
 
Share this answer
 
Wherever you got that code, you didn't think before you modified it.
1) it doesn't try to check the file content: this line:
var file = filepath;
does not read anything from the file.
You could try:
var file = File.ReadAllText(filepath);

2) Your regex matches everything. '.' in a Regex means "any character at all" so having a dot in your regex means it will always match every character:
[{(-_!@#$%^&*+<>,./\\?|':;~)}]
Is functionally identical to
[.]
Or even
.
This needs to be escaped with '\.'
3) Even if you fix that, the regex contains "special characters" like '^' - "start of line or string", and '$' - "end of line or string". Special characters need to be escaped in the same way that '.' needed to be.
4) Inside your loop: what do you think that does? your regex matches individual characters, and you then assume that they are automatically valid regexes?
This is the code that generates the error message: as soon as you regex matches the first '\' - which it will, very quickly given that you existing code parses the file path rather than the file content - the regex processor fails because that's an escape character and it needs to be followed by the character you are escaping.

Even if I believe that you aren't the person who posted this question twice already - which would be a HUGE leap of faith - then you need to learn that you can't just clip bits of code out, slam them together and get what you want: you need to think about what the code you are given does, and how that might form a basis for you requirements. You will almost never find code that you can just copy, paste, and submit as your homework and expect it to work as is.
 
Share this answer
 

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