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?