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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:48:29+00:00 2026-05-17T00:48:29+00:00

Edit: I’m really just curious as to how I can get this regex to

  • 0

Edit: I’m really just curious as to how I can get this regex to work. Please don’t tell me there are easier ways to do it. That’s obvious! 😛

I’m writing a regular expression (using Python) to parse lines in a configuration file. Lines could look like this:

someoption1 = some value # some comment
# this line is only a comment
someoption2 = some value with an escaped \# hash
someoption3 = some value with a \# hash # some comment

The idea is that anything after a hash symbol is considered to be a comment, except if the hash is escaped with a slash.

I’m trying to use a regex to break each line into its individual pieces: leading whitespace, left side of the assignment, right side of the assignment, and comment. For the first line in the example, the breakdown would be:

  • Whitespace: “”
  • Assignment left: “someoption1 =”
  • Assignment right: ” some value “
  • Comment “# some comment”

This is the regex I have so far:

^(\s)?(\S+\s?=)?(([^\#]*(\\\#)*)*)?(\#.*)?$

I’m terrible with regex, so feel free to tear it apart!

Using Python’s re.findAll(), this is returning:

  • 0th index: the whitespace, as it should be
  • 1st index: the left side of the assignment
  • 2nd index: The right side of the assignment, up to the first hash, whether escaped or not (which is incorrect)
  • 5th index: The first hash, whether escaped or not, and anything after it (which is incorrect)

There’s probably something fundamental about regular expressions that I’m missing. If somebody can solve this I’ll be forever grateful…

  • 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-17T00:48:29+00:00Added an answer on May 17, 2026 at 12:48 am

    The reason your regular expression isn’t matching as you want is because of the greedy matching behaviour of regular expressions: each part will match the longest substring such that the rest of the string can still be matched with the remainder of the regular expression

    What this means in the case of one of your lines with an escaped # is:

    • The [^\#]* (there’s no need to escape # btw) will match everything before the first hash, including the backslash before it
    • The (\\\#)* won’t match anything, as the string at this point starts with a #
    • The final (\#.*) will match the rest of the string

    A simple example to emphasise this potentially unintuitive behaviour: in the regular expression (a*)(ab)?(b*), the (ab)? will never match anything

    I believe this regular expression (based on the original one) should work: ^\s*(\S+\s*=([^\\#]|\\#?)*)?(#.*)?$

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

Sidebar

Related Questions

EDIT: There's now a doc page on this so this question is irrelevant, also
edit alright, I guess C is painful in nature--Just, this part, is particularly painful.
Edit: You can get the full source here: http://pastebin.com/m26693 Edit again: I added some
EDIT Leaving this for posterity, but nearly a year later, to get down voted,
EDIT: It looks like I'm completely misinformed. Please close this thread. Gah. For the
EDIT 1 I apologize but after reading the 2 suggested articles I still don't
EDIT: This post was originally specific to ASP.NET, but after thinking about it I'm
EDIT: Added debugging output with memory locations as suggested by PlasmaHH. I don't understand
Edit: The below question was answered by this . I have a new updated
Edit: I'm looking for solution for this question now also with other programming languages.

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.