I'm making a program to automate the writing of some C code, (I'm writing to parse strings into enumerations with the same name)
C's handling of strings is not that great.
So some people have been nagging me to try python.
I made a function that is supposed to remove C-style /* COMMENT */
and //COMMENT
from a string:
Here is the code:
def removeComments(string):
re.sub(re.compile("/*.*?*/",re.DOTALL ) ,"" ,string) # remove all occurance streamed comments (/*COMMENT */) from string
re.sub(re.compile("//.*?
" ) ,"" ,string) # remove all occurance singleline comments (//COMMENT
) from string
So I tried this code out.
str="/* spam * spam */ eggs"
removeComments(str)
print str
And it apparently did nothing.
Any suggestions as to what I've done wrong?
There's a saying I've heard a couple of times:
If you have a problem and you try to solve it with Regex you end up with two problems.
EDIT:
Looking back at this years later. (after a fair bit more parsing experience)
I think regex may have been the right solution.
And the simple regex used here "good enough".
I may not have emphasized this enough in the question.
This was for a single specific file. That had no tricky situations.
I think it would be a lot less maintenance to keep the file being parsed simple enough for the regex, than to complicate the regex, into an unreadable symbol soup. (e.g. require that the file only use //
single line comments.)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…