RedHawkDK
RedHawkDK

Reputation: 139

Regex specific dash pattern (Python)

I am trying to make a regex that recognizes a specific dash-pattern in a string.

String example: 1L34-1A345-12B45-1a or 01aB-5432A-0014z-20

Explained: four character - five characters - five characters - two characters

The string contains numbers, upper- and lowercase characters.

I came up with the following pattern and it does the trick, but I think it could be expressed a bit simpler.

pattern = '[A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9]-[A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9]-[A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9]-[A-Za-z0-9][A-Za-z0-9]'

Any ideas?

Upvotes: 0

Views: 49

Answers (1)

Stefan
Stefan

Reputation: 1934

Indeed, you can simplify your regex to e.g.

pattern = r'[A-Za-z0-9]{4}-[A-Za-z0-9]{5}-[A-Za-z0-9]{5}-[A-Za-z0-9]{2}'

Upvotes: 1

Related Questions