Skip to content

Instantly share code, notes, and snippets.

@z2s8
Created October 21, 2014 14:11
Show Gist options
  • Save z2s8/1473d68210459fb9c158 to your computer and use it in GitHub Desktop.
Save z2s8/1473d68210459fb9c158 to your computer and use it in GitHub Desktop.
re4lm
u0_a200@grouper:/ $ python
Python 3.2.2 (default, Jun 23 2014, 00:13:13)
[GCC 4.8] on linux-armv7l
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> re.match('^[a-z0-9]{8}$', '878h76t5')
<_sre.SRE_Match object at 0x405ec480>
>>> re.match('^[a-z0-9]{8}$', '8786h76t5')
>>> re.match('^[a-z0-9]{8}$', '8786h76X5')
>>> s='ide az input'
>>> if re.match('^[a-z0-9]{8}$', s) != None: print(True)
...
>>>
>>> s='6g5f4d3s'
>>> if re.match('^[a-z0-9]{8}$', s) != None: print(True)
...
True
>>>
@z2s8
Copy link
Author

z2s8 commented Oct 21, 2014

^[a-z0-9]{8}$ explained:

  • ^ beginning of the string
  • [a-z0-9] any single char which is between a-z or 0-9
  • {8} 8 times that
  • $ end of string

Also note that ^ and $ are necessary, because otherwise it would match '82838383838' too, as it contains our pattern.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment