#!/usr/bin/python3
# coding: utf-8

import re

# Un fragment de Moby Dick, par Melville.
text = '''
The pale Usher--threadbare in coat, heart, body, and brain; I see him
now. He was ever dusting his old lexicons and grammars, with a queer
handkerchief, mockingly embellished with all the gay flags of all
the known nations of the world. He loved to dust his old grammars; it
somehow mildly reminded him of his mortality.

"While you take in hand to school others, and to teach them by what
name a whale-fish is to be called in our tongue leaving out, through
ignorance, the letter H, which almost alone maketh the signification of
the word, you deliver that which is not true." --HACKLUYT

"WHALE.... Sw. and Dan. HVAL. This animal is named from roundness or
rolling; for in Dan. HVALT is arched or vaulted." --WEBSTER'S DICTIONARY
'''

def cherche (regexp_comme_texte, texte):
    cre = re.compile (regexp_comme_texte)

    # re.search renvoie None si on n'a pas de match.
    match = re.search (cre, texte)
    if match == None:
        # Bien sûr, c'est plutôt sale de renvoyer soit liste, soit
        # une chaîne de caractère, selon le succès de la recherche.
        # Vous pouvez suivre un autre style.
        return []
    # Si je suis ici on a trouvé un match.

    # Paramètre de group:
    #   0: tout
    #   1: le texte qui a matché la sous-expression entre le premier groupe de
    #      parenthèses
    #   2: le texte qui a matché la sous-expression entre le deuxième groupe de
    #      parenthèses
    partie_matchee = match.group (1)
    return partie_matchee

# Exemple:
print (cherche ("(ce texte n'est pas dans Moby Dick)", text))
print (cherche ('(mildly)', text))
print (cherche ('[ \n]em(bellish)(ed)?', text))
