def sensitive_numerics(number): number = re.sub(r'[\+\(]?\d[\d .\-\(\)]{6,}', r'xxx', number) # [0-9] matches a single digit in the range 0 through 9 (inclusive), {4} indicates that four such digits should occur in a row, # - means a hyphen, and | means an OR and separates the two patterns you mention. # '123-4-5648' '1-234-5-6789' number = re.sub('[0-9]-[0-9]{3}-[0-9]-[0-9]{4}|[0-9]{3}-[0-9]-[0-9]{4}', 'xxx', number) return number