Python Forum
Regular expression to fetch comments in C using Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regular expression to fetch comments in C using Python
#1
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?
Reply
#2
What have you tried?
Reply
#3
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?
Reply
#4
http://blog.ostermiller.org/find-comment
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#5
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Algorithm for extracting comments from Python source code Pavel1982 6 524 Feb-28-2024, 09:52 PM
Last Post: Pavel1982
  data validation with specific regular expression shaheen07 0 338 Jan-12-2024, 07:56 AM
Last Post: shaheen07
  Python beginner that needs an expression added to existing script markham 1 703 Sep-04-2023, 05:24 AM
Last Post: Pedroski55
  Recursive regular expressions in Python risu252 2 1,236 Jul-25-2023, 12:59 PM
Last Post: risu252
  How do I add comments from a text-file to an array of folders? clausneergaard 2 1,792 Feb-08-2023, 07:45 PM
Last Post: Larz60+
  Regular Expression search to comment lines of code Gman2233 5 1,668 Sep-08-2022, 06:57 AM
Last Post: ndc85430
  Inserting line feeds and comments into a beautifulsoup string arbiel 1 1,188 Jul-20-2022, 09:05 AM
Last Post: arbiel
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 1,666 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  Need help with my code (regular expression) shailc 5 1,924 Apr-04-2022, 07:34 PM
Last Post: shailc
  Regular Expression for matching words xinyulon 1 2,176 Mar-09-2022, 10:34 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020