Skip to content

Instantly share code, notes, and snippets.

@blackrobot
Created February 19, 2015 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blackrobot/5d85f5f34f43fcdfc9cd to your computer and use it in GitHub Desktop.
Save blackrobot/5d85f5f34f43fcdfc9cd to your computer and use it in GitHub Desktop.
import re
BARCODE_PATTERN = r"""
^ [\W_] * # Ignore any leading non-alphanumerics
([\d\s]+) # Capture any digits, or spaces
[\W_]*$ # Ignore any trailing non-alphanumerics
"""
BARCODE_REGEX = re.compile(BARCODE_PATTERN, re.VERBOSE)
def sanitize_barcode(barcode):
""" Remove non-alphanumerics from the start and end of the barcode string,
then remove any space characters which may be remaining inside the
barcode.
"""
return ''.join(BARCODE_REGEX.sub(r'\1', barcode).split())
################
# Normal input #
################
sanitize_barcode("'0123456789 ")
# '0123456789'
sanitize_barcode("'0123456789")
# '0123456789'
sanitize_barcode("0123456789")
# '0123456789'
#####################
# Leading bad chars #
#####################
sanitize_barcode(" $,)_* $'0123456789")
# '0123456789'
# Trailing bad chars
sanitize_barcode("'0123456789$ ,$)_* ")
# '0123456789'
#######################
# Whitespace inserted #
#######################
sanitize_barcode("'01 23 45 67 89")
# '0123456789'
################
# Bad Barcodes #
################
sanitize_barcode("'X0123456789")
# "'X0123456789"
sanitize_barcode("'01234Y56789")
# "'01234Y56789"
sanitize_barcode("'0123456789Z")
# "'0123456789Z"
sanitize_barcode(" 'X '0123456789$', '")
# "'X'0123456789$','"
######################
# Everything at once #
######################
sanitize_barcode(" $!@#$ '01 \r 23 \n 45 \t 67 \f 89 \v )(*&^% ")
# '0123456789'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment