#320 · Regular expressions · Intermediate · 75 XP · about 15 min
Before a support chat is saved, every payment card number in it must be hidden, keeping only the last four digits so the customer can still tell which card was meant. Write redact(text) that returns text with every card number redacted.
A digit run is a longest possible stretch of text made of digits, where two neighbouring digits may have one space or one dash between them. In "Card 4111 1111-1111 1234." the run is "4111 1111-1111 1234": the dot ends it.
*, and keep its spaces and dashes where they are.redact('Card: 4111 1111 1111 1234, thanks') # returns 'Card: **** **** **** 1234, thanks' redact('Paid with 5500-0000-0000-0004.') # returns 'Paid with ****-****-****-0004.' redact('Call 555-0187 about order 12345') # returns 'Call 555-0187 about order 12345'
text has at most 100,000 characters, all ASCII.Two spaces in a row, or a space next to a dash, end a run: "4111 1111" is two runs of four digits.
First solves only, ranked by stars and then time.