# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(\/)(\*)(.|\n)*?\2\1|(\/\/.*)"
test_str = ("#include<stdio.h>\n"
"#include<stdlib.h>\n"
"typedef struct Node\n"
"{\n"
" int data;\n"
" struct Node *next;\n"
" struct Node *prev;\n"
"}node;\n"
"void insert(node *pointer, int data)\n"
"{\n"
" /* Iterate through the list till we encounter the last node.*/\n"
" while(pointer->next!=NULL)\n"
" {\n"
" pointer = pointer -> next;\n"
" }\n"
" /* Allocate memory for the new node and put data in it.*/\n"
" pointer->next = (node *)malloc(sizeof(node));\n"
" (pointer->next)->prev = pointer;\n"
" pointer = pointer->next;\n"
" pointer->data = data;\n"
" pointer->next = NULL;\n"
"}\n"
"int find(node *pointer, int key)\n"
"{\n"
" pointer = pointer -> next; //First node is dummy node.\n"
" /* Iterate through the entire linked list and search for the key. */\n"
" while(pointer!=NULL)\n"
" {\n"
" if(pointer->data == key) //key is found.\n"
" {\n"
" return 1;\n"
" }\n"
" pointer = pointer -> next;//Search in the next node.\n"
" }\n"
" /*Key is not found */\n"
" return 0;\n"
"}\n"
"void delete(node *pointer, int data)\n"
"{\n"
" /* Go to the node for which the node next to it has to be deleted */\n"
" while(pointer->next!=NULL && (pointer->next)->data != data)\n"
" {\n"
" pointer = pointer -> next;\n"
" }\n"
" if(pointer->next==NULL)\n"
" {\n"
" printf(\"Element %d is not present in the list\\n\",data);\n"
" return;\n"
" }\n"
" /* Now pointer points to a node and the node next to it has to be removed */\n"
" node *temp;\n"
" temp = pointer -> next;\n"
" /*temp points to the node which has to be removed*/\n"
" pointer->next = temp->next;\n"
" temp->prev = pointer;\n"
" /*We removed the node which is next to the pointer (which is also temp) */\n"
" free(temp);\n"
" /* Beacuse we deleted the node, we no longer require the memory used for it .\n"
" free() will deallocate the memory.\n"
" */\n"
" return;\n"
"}\n"
"void print(node *pointer)\n"
"{\n"
" if(pointer==NULL)\n"
" {\n"
" return;\n"
" }\n"
" printf(\"%d \",pointer->data);\n"
" print(pointer->next);\n"
"}\n"
"int main()\n"
"{\n"
" /* start always points to the first node of the linked list.\n"
" temp is used to point to the last node of the linked list.*/\n"
" node *start,*temp;\n"
" start = (node *)malloc(sizeof(node));\n"
" temp = start;\n"
" temp -> next = NULL;\n"
" temp -> prev = NULL;\n"
" /* Here in this code, we take the first node as a dummy node.\n"
" The first node does not contain data, but it used because to avoid handling special cases\n"
" in insert and delete functions.\n"
" */\n"
" printf(\"1. Insert\\n\");\n"
" printf(\"2. Delete\\n\");\n"
" printf(\"3. Print\\n\");\n"
" printf(\"4. Find\\n\");\n"
" while(1)\n"
" {\n"
" int query;\n"
" scanf(\"%d\",&query);\n"
" if(query==1)\n"
" {\n"
" int data;\n"
" scanf(\"%d\",&data);\n"
" insert(start,data);\n"
" }\n"
" else if(query==2)\n"
" {\n"
" int data;\n"
" scanf(\"%d\",&data);\n"
" delete(start,data);\n"
" }\n"
" else if(query==3)\n"
" {\n"
" printf(\"The list is \");\n"
" print(start->next);\n"
" printf(\"\\n\");\n"
" }\n"
" else if(query==4)\n"
" {\n"
" int data;\n"
" scanf(\"%d\",&data);\n"
" int status = find(start,data);\n"
" if(status)\n"
" {\n"
" printf(\"Element Found\\n\");\n"
" }\n"
" else\n"
" {\n"
" printf(\"Element Not Found\\n\");\n\n"
" }\n"
" }\n"
" }\n\n\n"
"}")
matches = re.finditer(regex, test_str)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html