# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^\s*((?:const\s+|virtual\s+|friend\s+|volatile\s+){0,4}\s*?(?:\w+\s*::)?\s*[\w]+)\s+(\w+)\s*\([\s\S]*?\)\s*(const)?\s*{"
test_str = ("#include \"Port.h\"\n\n"
"namespace Port {\n\n"
" uint8_t lirePin(volatile uint8_t * pin)\n"
" {\n"
" return * pin;\n"
" }\n"
" /*********************************************************************\n"
" * retourne vrai si les pin du port pin sont ouvert\n"
" * pin = PINA a PIND\n"
" *********************************************************************/\n"
" bool lirePin(uint8_t pins, volatile uint8_t * pin)\n"
" {\n"
" return (*pin & pins);\n"
" }\n"
" /*********************************************************************\n"
" * Met la pin du port port a 5V\n"
" * port = PORTA a PORTD\n"
" *********************************************************************/\n"
" void ouvrirPin(uint8_t pins, volatile uint8_t * port)\n"
" {\n"
" *port |= pins;\n"
" }\n"
" /*********************************************************************\n"
" * Met la pin du port port a 0V\n"
" * port = PORTA a PORTD\n"
" *********************************************************************/\n"
" void fermerPin(uint8_t pins, volatile uint8_t * port)\n"
" {\n"
" *port &= ~pins;\n"
" }\n"
" /*********************************************************************\n"
" * Met la pin du port ddr en Entree\n"
" * ddr = DDRA a DDRD\n"
" *********************************************************************/\n"
" void pinEntree(uint8_t pins, volatile uint8_t * ddr)\n"
" {\n"
" *ddr &= ~pins;\n"
" }\n"
" /*********************************************************************\n"
" * Met la pin du port ddr en Sortie\n"
" * ddr = DDRA a DDRD\n"
" *********************************************************************/\n"
" const volatile friend std::void pinSortie(uint8_t pins, \n"
"volatile uint8_t * ddr)\n"
" {\n"
" *ddr |= pins;\n"
" }\n"
" /*********************************************************************\n"
" * Met le port ddr en Entree\n"
" * ddr = DDRA a DDRD\n"
" *********************************************************************/\n"
" void portEntree(volatile uint8_t * ddr) const\n"
" {\n"
" *ddr = ENTREE;\n"
" }\n"
" /*********************************************************************\n"
" * Met le port ddr en Entree\n"
" * ddr = DDRA a DDRD\n"
" *********************************************************************/\n"
" void portSortie(volatile uint8_t * ddr)\n"
" {\n"
" *ddr = SORTIE;\n"
" }\n"
" \n"
"}\n")
matches = re.finditer(regex, test_str, re.MULTILINE)
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