简体   繁体   中英

My For Loop Is Closing Too Soon with Regex.Replace

I have 1-10 CampaignId to change in my regex and add 1-10 inside the div tags I am trying to figure out why my for loop is closing too soon with the Regex code I have. It is only printing the 10th CampainId and 10th div tag in the HTML.

Here is the code.

protected void Page_Load(object sender, EventArgs e)
{
  string con = ("Data Source=Desktop-7k2gnco;Initial Catalog=trackingstats;"
    + "Integrated Security=SSPI");
  
  string queryString = @"
    select CampaignId,
           ConvertedCreative
    from CampaignAdCopy
    where CampaignId = 24896;
  ";

  using ( SqlConnection connection = new SqlConnection(con) )
  {
    SqlCommand command = new SqlCommand(queryString, connection);
    
    connection.Open();
    
    SqlDataReader reader = command.ExecuteReader();
    while (reader.Read())
    {             
      string CampaignId = reader["CampaignId"].ToString();
      string htmlCreate= reader["ConvertedCreative"].ToString();
      
      for ( int i = 1 ; i < 11 ; i++ )
      {
        string replacement = "<div style=\"z-indexer:1;background:green;width:2em;height:2em;text-align:center;color:white;\">"
          + i
          + "</div><a href=\"http://www.trackingstats.info/Process.aspx.c=/"
          + CampaignId
          + "&l="
          + i
          + "\""
          ;
        Regex regex = new Regex(
            "<a href=\"http://www.trackingstats.info/Process.aspx.c=24896&l="
            + i
            + "\"",
            RegexOptions.IgnoreCase
          );
        string result = regex.Replace( htmlCreate, replacement );
        
        Console.WriteLine(result);
        Label1.Text = result;
      }
                 
    }
  }
}

Label1.Text is overwriting the context. Try the following.

    while (reader.Read())
    {             
      string CampaignId = reader["CampaignId"].ToString();
      string htmlCreate= reader["ConvertedCreative"].ToString();
      StringBuilder strContents = new StringBuilder();
      for ( int i = 1 ; i < 11 ; i++ )
      {
        string replacement = "<div style=\"z-indexer:1;background:green;width:2em;height:2em;text-align:center;color:white;\">"
          + i
          + "</div><a href=\"http://www.trackingstats.info/Process.aspx.c=/"
          + CampaignId
          + "&l="
          + i
          + "\""
          ;
        Regex regex = new Regex(
            "<a href=\"http://www.trackingstats.info/Process.aspx.c=24896&l="
            + i
            + "\"",
            RegexOptions.IgnoreCase
          );
        string result = regex.Replace( htmlCreate, replacement );
        
        Console.WriteLine(result);
        strContents.AppendLine(result);
      }
      Label1.Text = strContents.ToString();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM