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

  • Home
  • SEARCH
  • 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 9068385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:10:43+00:00 2026-06-16T17:10:43+00:00

I am creating a blog site, where i will be allowing user to enter

  • 0

I am creating a blog site, where i will be allowing user to enter code inside [Code] Code Content [/Code]

There will be multiple [Code] blocks like this in one blog post.

I want to find each [Code] block using Regex and then replace it with

<pre>command

Also i want to replace &lt; and &gt; inside pre tag to < >

Now i found useful code which can help me through that but i am confused with Regex, can someone help me with this.

    static string ProcessCodeBlocks(string value)
{
    StringBuilder result = new StringBuilder();

    Match m = Regex.Match(value, @"\[pre=(?<lang>[a-z]+)\](?<code>.*?)\[/pre\]");
    int index = 0;
    while( m.Success )
    {
        if( m.Index > index )
            result.Append(value, index, m.Index - index);

        result.AppendFormat("<pre class=\"{0}\">", m.Groups["lang"].Value);
        result.Append(ReplaceBreaks(m.Groups["code"].Value));
        result.Append("</pre>");

        index = m.Index + m.Length;
        m = m.NextMatch();
    }

    if( index < value.Length )
        result.Append(value, index, value.Length - index);

    return result.ToString();
}
  • 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-16T17:10:45+00:00Added an answer on June 16, 2026 at 5:10 pm

    ..explanation from RegexBuddy:

    \[pre=(?<lang>[a-z]+)\](?<code>.*?)\[/pre\]
    
    Match the character “[” literally «\[»
    Match the characters “pre=” literally «pre=»
    Match the regular expression below and capture its match into backreference with name     “lang” «(?<lang>[a-z]+)»
       Match a single character in the range between “a” and “z” «[a-z]+»
          Between one and unlimited times, as many times as possible, giving back as needed     (greedy) «+»
    Match the character “]” literally «\]»
    Match the regular expression below and capture its match into backreference with name     “code” «(?<code>.*?)»
       Match any single character that is not a line break character «.*?»
          Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
    Match the character “[” literally «\[»
    Match the characters “/pre” literally «/pre»
    Match the character “]” literally «\]»
    

    To make it work for [Code][/Code], you would change it to this:

    \[code\](?<code>.*?)\[/code\]
    

    ..keeping in mind this will only work for single-line blocks. Also, there is only a code group.. there is no lang group anymore.. so remove that from the C#..

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

Sidebar

Related Questions

I am creating a blog engine that will require a user to enter the
I am creating a site much like a wordpress blog whereby the front page
I am creating an online blog website and for each blog post, I'd like
I am creating a blog for my site using Word Press. The only part
I've been creating a custom Tumblr blog theme for my site and all was
I am creating an web application which allows user to post some article/blog on
I'm creating a website template where the owner of the site will be able
I have created a random blog post link on my site and the code
I'd like if someone could give me some advice on creating this script, which
I'm creating a blog, and am storing user permissions (to post/edit/delete blog posts) in

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.