tags:

views:

102

answers:

3

what is the regular expressions that will identify the class of valid NRIC numbers (inclusive of the ending alphabets)

+3  A: 

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.

Daniel Cassidy
If I still had votes left today, my vote would go to this for the reminder about the single-letter checksum having to be calculated separately.
BoltClock
+2  A: 

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]$

E Brown
+1  A: 

Let us see.

  1. Alphabet for the first letter: [a-z] (We'll ignore case later)
  2. Seven digits: \d{7} (hint: d is for digits ;))
  3. 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.

Manoj Govindan