With re module, fullmatch is used to match only whole string
re
fullmatch
>>> expr = re.compile(r'[a-z\d]+(?:\.[a-z\d]+)+') >>> strings = ['abc.bcd.edsaf.asdfds', 'abc.asdf123.1234adf', 'abc.ad', ... 'abc', 'abc.', 'abc.132A', 'ASD', '1234', '1234ASF.'] >>> [bool(expr.fullmatch(s)) for s in strings] [True, True, True, False, False, False, False, False, False]
With regex module, you can use subexpression call to avoid duplication. Also, possessive quantifiers can be used here to speed up things.
regex
>>> expr = regex.compile(r'([a-z\d]++)(?:\.(?1))++') >>> [bool(expr.fullmatch(s)) for s in strings] [True, True, True, False, False, False, False, False, False]
Hey thanks for sharing this!
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
With
re
module,fullmatch
is used to match only whole stringWith
regex
module, you can use subexpression call to avoid duplication. Also, possessive quantifiers can be used here to speed up things.Hey thanks for sharing this!