Python Forum
Regular expression to fetch comments in C using Python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Regular expression to fetch comments in C using Python (/thread-1732.html)



Regular expression to fetch comments in C using Python - pikkip - Jan-23-2017

I am coding a program in Python to fetch comments in a C program( both single and multiline). What regular expression should be used for this?


RE: Regular expression to fetch comments in C using Python - micseydel - Jan-23-2017

What have you tried?


RE: Regular expression to fetch comments in C using Python - pikkip - Jan-24-2017

I am having issues with lines containing the code and the comment like:
printf("hai)    // prints hai
c = c + 1     /*  htshgbxbvxvbxvxxzv

                        fgdhdhgdghfcncnbc

                  arabcnbchcjc     */
I am not able to categorize the kind of characters that can come up before the comment in 1 and 2. ( . )* doesn't help. Is there any other way to sum up them all rather than specifying them individually?


RE: Regular expression to fetch comments in C using Python - Ofnuts - Jan-24-2017

http://blog.ostermiller.org/find-comment


RE: Regular expression to fetch comments in C using Python - wavic - Jan-24-2017

Well, chek if there is '//' or '*/' in the row. If it is, split the row by '//' or '*/', get the text with in the list which appers to be the comment ( this sould be row[1] ). Then if that is '*/' just take every row until you find '*/'. This is step by step. You can rid off the white space characters using str.strip() function. If you need to.