Quantcast
Viewing all articles
Browse latest Browse all 14069

REGEX to match to identifier

I'm currently trying to write tests for a client of an API. Specifically, I want to write a test which looks if identifier has the format I want it to have.

The format is the following: s3i:********-****-****-****-************. Each * is a random character from the set [0-9a-f], the rest is fixed.

I could do check like the following, but that would be clunky:

import itertoolsdef test_identifier_format(ident: str):    assert ident.startswith("s3i:")    for i in itertools.chain(range(4, 12), range(13, 16), range(17,20), range(21, 24), range(25, 40))        assert ident[i] in "0123456789abcdef"    assert "-" == ident[12] == ident[16] == ident[20] == ident[24]

So my question: How can you do that more elegantly? Would regular expressions be the right choice?


Viewing all articles
Browse latest Browse all 14069

Trending Articles