locked
Regular expressions and C# - replace pattern with pattern RRS feed

  • Question

  • Sorry, my questions are not so difficult. But can't find answers how to work with Regular Expressions in C#.

    What mean sign @ before blockquote in C# Regex?

    And how to replace pattern with pattern? For example replace <img ...> with [img ....]

     

     


    Friday, December 9, 2011 7:24 AM

Answers

  • > And how to replace pattern with pattern? For example replace <img ...> with [img ....]
     
       

    var re = new Regex("<(?<tag>img.+?)>",  RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled);
    var str = re.Replace("text <img src='some'> text", "[${tag}]");
    
      

    Friday, December 9, 2011 11:19 AM

All replies

  • Have found this:

       string mes;
               mes = "&lt;img&gt;";
    
                mes = Regex.Replace(mes, @"&lt;img*&gt;", delegate(Match match)
                {
                    string v = match.ToString();
                    return "[" + v[1] + "]";
                });
    
                MessageBox.Show(mes);
    

    but what to place in v[1]?

    Couse img tag can have some attributes inside...

     

    Friday, December 9, 2011 10:13 AM
  • Have found. May be will gonna use something like this:

               string mes;
               mes = "&lt;h1 style= &gt;";
    
               string res;
                res = Regex.Replace(mes, @"&lt;h1.*&gt;", delegate(Match match)
                {
                    string v = match.ToString();
                    return "<" + v.Substring(4,v.Length-8)+">";
                 
                });
    
                MessageBox.Show(res);
    

    Friday, December 9, 2011 10:32 AM
  • > And how to replace pattern with pattern? For example replace <img ...> with [img ....]
     
       

    var re = new Regex("<(?<tag>img.+?)>",  RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled);
    var str = re.Replace("text <img src='some'> text", "[${tag}]");
    
      

    Friday, December 9, 2011 11:19 AM
  • Thank you!

    And I have write such one:

     

    messs = createHTML(messs, "&lt;img[a-zA-z0-9\t\n .:;=\"/]*&gt;");
    

    and:

     

     

       string createHTML(string mes,string pattern){
           string res;
    
           res = Regex.Replace(mes, @pattern, delegate(Match match)
           {
               string v = match.ToString();
               return "<" + v.Substring(4, v.Length - 8) + ">";
    
           },RegexOptions.IgnoreCase);
    
           return res;
    
        }
    

    But now I thinking about how to check that string does not contained JavaScript?

     

    Add match inside delegate? Not a cute code....

    Friday, December 9, 2011 12:50 PM
  • > createHTML(messs, "&lt;img[a-zA-z0-9\t\n .:;=\"/]*&gt;"); [...] Add match inside delegate? ...
     
     
     
       
    var html = createHTML("&lt;img src='url' &gt;\n&lt;br/>");
    ...
    
    string createHTML(string str)
    {
        MatchEvaluator me = m => string.Concat("<", m.Groups["tag"].Value, ">");
        return Regex.Replace(str, "&lt;(?<tag>.+?)(&gt;|>)", me, 
            RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled);
    }
    
     


    • Edited by Malobukv Friday, December 9, 2011 1:51 PM
    Friday, December 9, 2011 1:51 PM
  • > But now I thinking about how to check that string does not contained JavaScript?

     

    var re = new Regex(@"(<|&lt;|&\#60;)script\b",
        RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled);
    
     

    • Proposed as answer by Malobukv Friday, December 9, 2011 1:59 PM
    Friday, December 9, 2011 1:59 PM
  • Yea, that's right. But I want to check this at the same regex where i searching for img.

    I think that your first reply is the best. Gonna try it now... 

    Friday, December 9, 2011 2:30 PM
  • Will mark your answer as answer. ))

    But in code better will use my pattern.  I have add here ? - couse there can be some chars after img or can be not

    "&lt;img[a-zA-z0-9\t\n -.:;=\"/]*?&gt;"

     

     

     

     

    Friday, December 9, 2011 2:52 PM
  • > But I want to check this at the same regex where i searching for img.  
     

      
    could you please provide some of the possible texts?
    but i think it would be better to work with standalone patterns.
    and do search matches in parallel.
      
      

    Friday, December 9, 2011 2:55 PM
  • Sorry that so late.

    Offcourse I can! ) It's simple easy:

    "&lt;img src="javascript:alert('css');"&gt;"

    So, I want to replace when text match pattern "&lt;(?<tag>.+?)(&gt;|>)" and not contained "script" at the same time.

    Have found conditional OR | but can't find AND... Anyway, thank you Malobukv you have helped! ( ty napisal ne tak uj i malo bukv ))

    Monday, December 12, 2011 7:58 AM