Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have this text "<set><value><name>" in a file, now I need to remove the character "<" and ">" which is in between the text. I’m trying with the below code:

C#
private void button1_Click(object sender, EventArgs e)
{
sr = new StreamReader(sam);
 textBox1.Text = sr.ReadLine();
string pattern = "\"(?=[<\"]+,)[>\"]+\"";
                string result = Regex.Replace(textBox1.Text, pattern, m => m.Value.Replace("<", " "));
                textBox2.Text = result;
sr.Close();
}

I’m confused with this regular expression and I’m getting the same output
"<set><value><name>"
please help....
Posted
Updated 16-Sep-17 20:38pm
v3

As seen from your question, you want to replace "<" and ">" with a space. For this purpose the following code can be used
C#
string fileText = System.IO.File.ReadAllText(filePath);
fileText = System.Text.RegularExpressions.Regex.Replace(fileText,"(\"<\"|\">\")"," ");
System.IO.File.WriteAllText(filePath,fileText);
 
Share this answer
 
v4
C#
//This method is not really applicable in this case
// But it can be used when there are a lot of non-printable characters
//that need to be removed, such as control and formatting characters
public static string CleanText(string message, char[] prohibitedCharacters)
{
   List<char> messageList = message.ToCharArray().ToList();
   messageList.RemoveAll(c => (Array.IndexOf(prohibitedCharacters, c) != -1));
   string cleanedString = string.Join(string.Empty, messageList);
    return cleanedString;
}
 
Share this answer
 
Comments
Graeme_Grant 17-Sep-17 3:24am    
You have answered a 5-year-old question... did you mean to?
George Swan 17-Sep-17 3:35am    
Whoops, sorry. Didn't realise. I'll leave it up as it may be useful. Best wishes, George.
George Swan 17-Sep-17 3:39am    
I can see what has happened now. The OP updated his question.
CHill60 17-Sep-17 9:49am    
Nope. A non - answer was posted and then deleted. OP'S update was back in 2012
George Swan 17-Sep-17 12:18pm    
Ok, thanks. In any event, there was some activity that caused the question to get through my 'Active' filter.
You can clear the special characters using the below customized function as:

C#
private static string GetCleanString (string src)
{
    int i = 0;
    while (i < src.Length)
    {
        if (src[i] < 32 || src[i] > 127)
        {
            int pos = CHAR_REPLACE_SRC.IndexOf(src[i]);
            if (pos >= 0)
                src = src.Replace(CHAR_REPLACE_SRC[pos], CHAR_REPLACE_DST[pos]);
            else
                src = src.Remove(i, 1);
        }
        else
            i++;
    }
    return src.Replace("\"", "").Replace("?", "").Replace(":", "").Replace("&", "_").Replace("\\", "_").Replace("/", "_");
}
 
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