what is the regular expressions that will identify the class of valid NRIC numbers (inclusive of the ending alphabets)
Assuming you mean the Singaporean National Registration Identity Card, try:
^[SFTG]\d{7}[A-Z]$
This follows the structure documented by Wikipedia.
Note that the last letter is a checksum, and that if you want to check the checksum you’ll have to do so separately.
Regex patterns for many common uses can be found at regexlib.com. For NRIC try:
http://www.regexlib.com/Search.aspx?k=nric
This suggests a pattern of:
^[SFTG]\d{7}[A-Z]$
Let us see.
- Alphabet for the first letter:
[a-z]
(We'll ignore case later) - Seven digits:
\d{7}
(hint: d is for digits ;)) - Another alphabet:
[a-z]
Putting them together we get: [a-z]\d{7}[a-z]
. In python this would be:
import re
obj = re.compile('[a-z]\d{7}[a-z]', re.IGNORECASE)
obj.match('S1234567E')
You don't have to compile()
the regular expression if you are planning to use it only once. But in case you are planning to match the same expression against multiple strings then it would be a good idea to compile it.
Reference: documentation for the re
module.