Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8295715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:42:28+00:00 2026-06-08T14:42:28+00:00

I’m working on formatting C# code in HTML. I’m trying to replace tabs/indents with

  • 0

I’m working on formatting C# code in HTML. I’m trying to replace tabs/indents with 4 spaces.

Here’s an example.

protected void Page_Load(object sender, EventArgs e)
{
    Response.Write("Hello World");
}

I need to replace the tab before Response.Write with 4 spaces.

I tried things like ^\t, with different variations, tried ^\s\s\s\s. I thought this would be simple, but nothing I’ve tried seemed match the tabs.

What am I doing wrong?

Thank you!

Edit

I copy directly from VS into TextBox1.

enter image description here

As you can see, there are no actual tabs (\t) in the TextBox value, which is the root of the problem. As noted in my comments, the spaces with ^ did work (for the first line only).

So my final regex will look like… this: “\s\s\s\s”.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T14:42:32+00:00Added an answer on June 8, 2026 at 2:42 pm

    This should do what you want:

    Regex regex = new Regex(@"^\t+", RegexOptions.Multiline);
    s = regex.Replace(s, m => new string(' ', 4 * m.Value.Length));
    

    See it online: ideone


    Update

    Here’s a version for ASP.NET Web Forms that runs in Visual Web Developer 2010 Express:

    Default.aspx

    <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
        CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
    
    <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"></asp:Content>
    <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
        <asp:TextBox ID="TextBox1" runat="server" Height="99px" Width="500px" TextMode="MultiLine"></asp:TextBox>
    </asp:Content>
    

    Default.aspx.cs

    using System;
    using System.Text.RegularExpressions;
    
    namespace WebApplication1
    {
        public partial class _Default : System.Web.UI.Page
        {
            protected void Button1_Click(object sender, EventArgs e)
            {
                string code = "protected void Page_Load(object sender, EventArgs e)\n{\n\tResponse.Write(\"Hello World\");\n}";
                Regex regex = new Regex(@"^\t+", RegexOptions.Multiline);
                TextBox1.Text = regex.Replace(code, m => new string('*', 4 * m.Value.Length));
            }
        }
    }
    

    Result after clicking button:

    protected void Page_Load(object sender, EventArgs e)
    {
    ****Response.Write("Hello World");
    }
    

    The asterisks are there only to make it easy to see that the tabs have been replaced correctly with spaces. Change the '*' to ' ' to get spaces instead of asterisks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
i got an object with contents of html markup in it, for example: string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.