0

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?

1 Answer 1

1

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}'
Sign up to request clarification or add additional context in comments.

2 Comments

you can even simplify store this [A-Za-z0-9] in a variable and use varible to create regex
@Stefan Super! Just what I was looking for. And deadshot, good idea with the variable! Have a nice day! Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.