Created
February 19, 2015 20:16
-
-
Save blackrobot/5d85f5f34f43fcdfc9cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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