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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:25:19+00:00 2026-05-11T20:25:19+00:00

Although this question is similar to this thread I think I might be doing

  • 0

Although this question is similar to this thread

I think I might be doing something wrong at the time of constructing the code with the Regular Expression.

I want to match anything in a line up to a comment (“#”) or the end of the line (if it doesn’t have a comment).

The regex I am using is: (.*)(#|$)

(.*) = Everything
(#|$) = comment or end of line

The code:

OPTION = re.compile(r'(?P<value>.*)(#|$)')
file = open('file.txt')
lines = file.read()
for line in lines.split('\n'):
    get_match = OPTION.match(line)
    if get_match:
        line_value = get_match.group('value')
        print "Match=  %s" % line_value

The above works but does not strip out the comment.
If the file has a line like:

this is a line   # and this is a comment

I still get the whole line when running the code.

Am I missing additional values/information in the regular expression or do I need to have a change on the code?

  • 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-11T20:25:20+00:00Added an answer on May 11, 2026 at 8:25 pm

    The * is greedy (consumes as much of the string as it can) and is thus consuming the entire line (past the # and to the end-of-line). Change “.*” to “.*?” and it will work.

    See the Regular Expression HOWTO for more information.

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

Sidebar

Related Questions

Although somewhat related to this question , I have what I think is a
Although this is pretty basic, I can't find a similar question, so please link
Although this question and this question are close to what I'm asking, I believe
I have a HashMap (although I guess this question applies to other collections) of
I admit this is not strictly a programming question, although I do use my
I've seen this format used for comma-delimited lists in some C++ code (although this
I know this question is similar to several previous ones, but I can't find
This question is about a whole class of similar problems, but I'll ask it
I've read every response I could fine on SO before posting this question. Although
The accepted answer to this question , and a similar discussion at work today

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.