Try this out in the Regex playground: Link
Regex: ([\d]{4})[- ](0?[1-9]|1[0-2])[- ](\d{1,4})\b
Explanation: Matches year as a 4-digit number, month as an integer between 1-12 (including leading zeros), and customer id as an integer from 0-9999; the values can be separated by either dashes or spaces. The groups will be captured as (year, month, customer_id)
in that order.
Python demo:
import re
from typing import Optional, NamedTuple
invoice_re = re.compile(r'([\d]{4})[- ](0?[1-9]|1[0-2])[- ](\d{1,4})\b')
# NamedTuple that contains the invoice data
Invoice = NamedTuple('Invoice', year=int, month=int, customer_id=int)
def parse_invoice(invoice: str) -> Optional[Invoice]:
"""Parse an invoice, and return a tuple of (year, month, customer_id)"""
result = invoice_re.search(invoice)
return Invoice(*map(int, result.groups())) if result else None
s1 = 'Payment of invoice nr.2021-3-5450'
s2 = 'Invoice 2021 3 27 has been paid'
print(parse_invoice(s1))
print(parse_invoice(s2))