PIN Code stands for Postal Index Code. It represents a unique postal code using which you can identify a region or area or locality depending on how it is defined. In this article let’s understand how we can create a regex for PIN Code and how regex can be matched for PIN Code.
Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of characters that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.
Structure of India PIN Code
- It has to be six digits.
- It should not start with zero.
- First digit of the pin code must be from 1 to 9.
- Next five digits of the pin code may range from 0 to 9.
- It should allow only one white space, but after three digits, although this is optional.
Regex for checking if India PIN Code is valid or not
Regular Expression-
/^[1-9]{1}[0-9]{2}\s{0, 1}[0-9]{3}$/gm
Test string examples for the above regex-
Input String | Match Output |
---|---|
33 43 22 | does not match |
121212 | matches |
000121 | does not match |
454 322 | matches |
In case you want to skip validating the space inbetween, you can use the following regular expression-
/^[1-9]{1}[0-9]{2}[0-9]{3}$/gm
Here is a detailed explanation of the above regex-
/^[1-9]{1}[0-9]{2}\s{0,1}[0-9]{3}$/gm
^ asserts position at start of a line
Match a single character present in the list below [1-9]
{1} matches the previous token exactly one time (meaningless quantifier)
1-9 matches a single character in the range between 1 (index 49) and 9 (index 57) (case sensitive)
Match a single character present in the list below [0-9]
{2} matches the previous token exactly 2 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
{0,1} matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
Match a single character present in the list below [0-9]
{3} matches the previous token exactly 3 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
$ asserts position at the end of a line
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
Hope this article was useful to match India PIN code number regex pattern. In conclusion, understanding the concept of PIN Codes and how to create and match regex patterns for India’s Postal Index Codes is essential. Regex, a powerful text manipulation tool, can be utilized for various purposes, including pattern recognition and data validation. By delving into the structure of India’s PIN Codes and exploring the provided regex examples, you can effectively validate and process these crucial codes in different applications.