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 6109309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:23:25+00:00 2026-05-23T14:23:25+00:00

I am trying to parse the following line: \# TEST #comment hello world In

  • 0

I am trying to parse the following line:

"\#" TEST #comment hello world

In my input, the #comment always comes at the end of the line. There may or may not be a comment, but if there is, its always in the end of the line.

I used the following Regex to parse it:

(\#.+)?

I have the RegexOption.RightToLeft on. I expected it to pull #comment hello world. But instead it is pulling "#" TEST #comment hello world"

Why is my Regex expression not pulling the right thing and what is the valid Regex expression I need to make it pull correctly?

  • 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-05-23T14:23:25+00:00Added an answer on May 23, 2026 at 2:23 pm

    I think you’ll find too many edge cases when trying to pull this off with regular expressions. Dealing with the quotes is what really complicates things, not to mention escape characters.

    A procedural solution is not complicated, and will be faster and easier to modify as needs dictate. Note that I don’t know what the escape characters should be in your example, but you could certainly add that to the algorithm…

    string CodeSnippet = Resource1.CodeSnippet;
    StringBuilder CleanCodeSnippet = new StringBuilder();
    bool InsideQuotes = false;
    bool InsideComment = false;
    
    Console.WriteLine("BEFORE");
    Console.WriteLine(CodeSnippet);
    Console.WriteLine("");
    
    for (int i = 0; i < CodeSnippet.Length; i++)
    {
        switch(CodeSnippet[i])
        {
            case '"' : 
                if (!InsideComment) InsideQuotes = !InsideQuotes;
                break;
            case '#' :
                if (!InsideQuotes) InsideComment = true;
                break;
            case '\n' :
                InsideComment = false;
                break;                       
        }
    
        if (!InsideComment)
        {
            CleanCodeSnippet.Append(CodeSnippet[i]);
        }
    }
    
    Console.WriteLine("AFTER");
    Console.WriteLine(CleanCodeSnippet.ToString());
    Console.WriteLine("");
    

    This example strips the comments away from the CodeSnippet. I assumed that’s what you were after.

    Here’s the output:

    BEFORE
    "\#" TEST #comment hello world
    "ab" TEST #comment hello world
    "ab" TEST #comment "hello world
    "ab" + "ca" + TEST #comment
    "\#" TEST
    "ab" TEST
    
    AFTER
    "\#" TEST
    "ab" TEST
    "ab" TEST
    "ab" + "ca" + TEST
    "\#" TEST
    "ab" TEST
    

    As I said, you’ll probably need to add escape characters to the algorithm. But this is a good starting point.

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

Sidebar

Related Questions

I'm trying to change instances of the following line: URL: http://www.google.com/?s= test to URL:
I am trying to parse a date in c# and have the following line
I'm trying to parse the following json line in jquery: [{ pk: 19, model:
The following line: page_source = Net::HTTP.get(URI.parse(http://not-a-real-url.com)) When passed a url that is properly formatted,
I am trying to parse the following XML with javascript: <?xml version='1.0' encoding='UTF-8'?> <ResultSet>
So I'm trying to parse the following XML document with C#, using System.XML: <root
I'm trying to parse a 6,000 line 500 KB file into an array so
Is the following format correct for XML? I am trying to parse the file
I am trying to create a RegEx expression that will successfully parse the following
I'm trying to parse the following HTML from the following URL: http://md5.rednoize.com/?q=fbade9e36a3f36d3d676c1b808451dd7 The code:

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.