Editor

Redact Card Numbers

#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.

  • A run with 13 to 19 digits is a card number: replace each of its digits except the last four with *, and keep its spaces and dashes where they are.
  • Runs with fewer than 13 or more than 19 digits stay as they are.

Examples

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'

Constraints

  • text has at most 100,000 characters, all ASCII.

Note

Two spaces in a row, or a space next to a dash, end a run: "4111 1111" is two runs of four digits.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.