locked
Swapping out <title>t1</title><title>t2</title> with <title>new title</title><title>new title</title> RRS feed

  • Question

  • In .NET, how can I Regex.Replace() the following:

    <title>t1</title><title>t2</title>

    With the following:

    <title>new title</title><title>new title</title>

    How's this regex expression? "(<title[^>]*>).*</title>"
    Ian Davis
    Thursday, March 19, 2009 9:40 PM

All replies

  • please try this:

          string v = "<title>t1</title><title>t2</title>";     
          Regex reg = new Regex(@"<title>.+<\/title>");     
          string res = reg.Replace(v,"<title>new title</title>");     
     

    HTH
    Ivo Stoykov
    • Proposed as answer by ivostoykov Friday, March 20, 2009 8:29 AM
    Friday, March 20, 2009 8:29 AM
  • ivostoykov said:

    please try this:

          string v = "<title>t1</title><title>t2</title>";     
          Regex reg = new Regex(@"<title>.+<\/title>");     
          string res = reg.Replace(v,"<title>new title</title>");     
     

    HTH
    Ivo Stoykov


    Ivo, thank you for your response!

    res ends up being "<title>new title</title>"

    I'm looking for desired result = "<title>new title</title><title>new title</title>"

    Is this possible?  Or do I need to loop through a collection of Regex matches and replace them one by one.  There must be a magical regex expression that could handle it!  Thanks.


    Ian Davis
    Friday, March 20, 2009 2:30 PM