Skip to content

I want to replace number in two tag to double with regex in

mohamed salim

Asked Jan 6, 2017, 6:22 AM EST1 answer
Share
LinkedInX
I am bit confused writing the regex for finding the Text between the two delimiters
and replace the text with another text in c#,how to replace?
pop.txt:
25
Result of my code
25,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00
500
60,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00
80
my code:
C#
 string text = File.ReadAllText("pop.txt");

            try
            {


                var matches = Regex.Matches(text, pattern);

                var result = (from Match m in matches select (m.Groups[2].ToString())).ToList();


                foreach (var item in result)
                {
                  var a = Convert.ToDouble(item).ToString("#,##.00");
                    string s = String.Format("{0:0.##}", item);
                    //textBox4.Text += a  + Environment.NewLine;

                    text = text.Replace(item,a);


                }

            }
            catch (Exception ex)
            {
                // MessageBox.Show(ex.Message);
                //return;
            }
            File.WriteAllText("resultat.txt", text);

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Dear mohamed salim,
    I think you are trying to replace the data with in html tags
    So kindly try this below code
    1. private string replaceString = "";  
    2. public string Parse(string content)  
    3. {  
    4.     const string regTagName = @"<.[^>]*>";  
    5.       
    6.     Regex reg = new Regex(@"(" + regTagName + ")|(geekzilla)",  
    7.                      RegexOptions.IgnoreCase | RegexOptions.Multiline);  
    8.   
    9.     // this is what I'd like to replace the match with  
    10.     replaceString = "$1";  
    11.   
    12.     // do the replace  
    13.     content = reg.Replace(content, new MatchEvaluator(MatchEval));  
    14.   
    15.     return content;  
    16. }  
    17.   
    18. protected string MatchEval(Match match)  
    19. {  
    20.     if (match.Groups[1].Success)   
    21.     {  
    22.         // the tag  
    23.         return match.ToString();  
    24.     }  
    25.     if (match.Groups[2].Success)   
    26.     {  
    27.         // the text we're interested in  
    28.         return Regex.Replace(match.ToString(), "(.+)", replaceString);  
    29.     }  
    30.     // everything else  
    31.     return match.ToString();  
    32. }  
    All the best 
    0