uhexos
uhexos

Reputation: 391

Regex pattern to prevent two similar characters from being next to each other

Vanity Formats [img]
To make matters worse, vain fleshlings will sometimes represent phone numbers with words. These so-called vanity phone numbers start with 1 and the area code (ie. 1-123-) but after that they will continue with a mix of letters (upper or lowercase), numbers and dashes. They are restricted to using exactly 7 letters and numbers (following the 1 and area code), but dashes can be inserted at any point other than immediately after another dash. Flahlings make things so complicated!

I have been working on this puzzle; I have been able to handle the first set of integers and dashes to work, but I cannot get the alpha numeric section working. I am using python 3 and this is what I have so far:

vanity_number = r'[+]?(1-)[0-9]{3}-[a-zA-Z0-9-]{7}'

This kinda works to a certain degree, but I need it to count dashes separately from alpha numeric, so that something like 1-345-qpG-8s-vd still fits the pattern. Thanks for any help you can provide.

Upvotes: 0

Views: 473

Answers (1)

bobble bubble
bobble bubble

Reputation: 18490

The last part should consist of 7 letters. If I understand right, each can be followed by an optional dash but there must not be two dashes bordering to each other and probably should not end in dash.

^\+?1-\d{3}-(?:[A-Za-z\d]-?){7}\b$

Remove the word boundary \b at the end if ending in a dash would be allowed.

Here is a demo at regex101

Upvotes: 2

Related Questions