What is the correct regex for matching values generated by uuid.uuid4().hex?

后端 未结 4 1071
一生所求
一生所求 2021-02-05 00:38

How do I validate that a value is equal to the UUID4 generated by this code?

uuid.uuid4().hex

Should it be some regular expression? The values

4条回答
  •  不要未来只要你来
    2021-02-05 01:00

    Easy enough:

    import re
    uuid4hex = re.compile('[0-9a-f]{32}\Z', re.I)
    

    This matches only for strings that are exactly 32 hexadecimal characters, provided you use the .match() method (searches from the start of the string, see .search() vs. .match()). The \Z matches the end of the string (vs. $ which would match at the end of a string or a newline).

提交回复
热议问题