Sujet : Re: Code improvement question
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.lang.pythonDate : 19. Nov 2023, 11:04:06
Autres entêtes
Organisation : Stefan Ram
Message-ID : <regexp-20231119110325@ram.dialup.fu-berlin.de>
References : 1 2 3 4 5 6 7 8 9 10 11
ram@zedat.fu-berlin.de (Stefan Ram) writes:
Thomas Passin <list1@tompassin.net> writes:
re.findall(r'\b[0-9]{2,7}-[0-9]{2}-[0-9]{2}\b', txt)
Or,
def repeat_preceding( min=None, max=None, count=None ):
''' require that the preceding regexp is repeated
a certain number of times, use either min and max
or count '''
return '{' + str( count )+ '}' if count else \
'{' + str( min )+ ',' + str( max )+ '}'
digit = '[0-9]' # match a decimal digit
word_boundary = r'\b' # match a word boundary
a_hyphen = '-' # match a literal hyphen character
def digits( **kwargs ):
''' A certain number of digits. See 'repeat_preceding' for
the possible kwargs. '''
return digit + repeat_preceding( **kwargs )
def word( regexp: str ):
''' something that starts and ends with a word boundary '''
return word_boundary + regexp + word_boundary
my_regexp = \
word \
( digits( min=2, max=7 ) + a_hyphen +
digits( count=2 ) + a_hyphen +
digits( count=2 ))