Skip to content

Instantly share code, notes, and snippets.

@ryukinix
Created April 15, 2016 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryukinix/a9e6d774648555d8f4c6e57bef82cf2a to your computer and use it in GitHub Desktop.
Save ryukinix/a9e6d774648555d8f4c6e57bef82cf2a to your computer and use it in GitHub Desktop.
Regex example of python n-digit number with n fixed
import re
import random
def num_matcher(num, _len=6):
pattern = re.compile(r"^[0-9]{{{_len}}}".format_map(locals()))
return bool(pattern.match(num))
value = random.randint(0, 1000000)
print(value)
print(num_matcher(str(value)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment