Click here to Skip to main content
15,899,018 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to add new regex to my code, which prints from list (with streamreader).
Where to add regex which removes these characters( - : _ |) from file and regex to add header to the file.

regex of replacing char: line.Replace(@"- : _ |", string.Empty);

regex for adding header:

C#
using (StreamWriter file = new StreamWriter(fs))                    
{
    file.WriteLine( "Number,User,Function,Message"); 

    for (int index = 0; index < pr.Length; index++)
    {
      
        file.WriteLine("{0},\"{1}\",{2},{3}",
           pr[s].Number, pr[s].User,
           pr[s].Function, pr[s].Message; 
    }
}


What I have tried:

C#
using (StreamWriter writer = new StreamWriter(path2))
            {
                foreach (string l in inactive)
                {

           //line.Replace(@"- : _ |", string.Empty); 
                    writer.WriteLine(l);
}
}
Posted
Updated 17-Nov-17 1:58am
v2
Comments
Richard MacCutchan 17-Nov-17 7:41am    
What is wrong with using the string.Replace method?
anabeth4 17-Nov-17 7:46am    
i tried but it doesn't remove the characters

Quote:
i want to remove not replace those characters

That's what replacing them with an empty string does - it removes the characters.
If you want to remove all those characters regardless of where rgey appear, ratehr than as a single string, you have teh wrong regex.
Your regex will change this
Hello -:_|There!
To this:
Hello There!
But will leave this unchanged:
Hello -:There!_|

If you want to remove all instances of any of the characters, you need this regex:
[-:_|]+


Get a copy of Expresso[^] - it's free, and it examines and generates Regular expressions.

But that code is looking very much like the code used by a member who is starting to become rather a Help Vampire / Troll because he refuses to learn anything and think for himself ... I smell a sock puppet here...
 
Share this answer
 
Comments
anabeth4 17-Nov-17 8:32am    
maybe different people same hw? this page supposed to help others not to make irony ;)
thnx!
You should try to replace
@"- : _ |"

with
@"[- : _ |]"


Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
 
Share this answer
 
You cannot use string.Replace this way (using a global string); you have to use a string.Replace for each character to replace.
Example:
C#
l = l.Replace("-", "").Replace(":", "").Replace("_", "").Replace("|", "");

Notes:
- string.Replace does not proceed to an in-place editing of the variable, you have to cast the result of the method to the variable again (l = ...).
- You can chain the calls to the Replace method.
- In the code block you showed, in the commented line, you seem to use a line which does not match the l variable defined in your foreach loop.
There are other options to do the same, using regular expressions for example, but I think some directions already have been given to you on this subject in some other posts.
Hope this helps. Kindly.
 
Share this answer
 
v2

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