Regular Expressions to Validate Account Office Reference Number
Given some Account Office Reference Number, the task is to check if they are valid or not using regular expressions. Rules for the valid Account Office Reference Number are:
- It is an alphanumeric string containing upper-case letters and digits.
- Accounts Office Reference Number is a unique, 13-character code.
- It starts with digits (0-9) and ends with digits.
- Its first three places are reserved for digits.
- After the first three characters, two places are occupied by uppercase alphabet letters (A-Z).
- After the first five characters, the next 8 places are reserved for digits (0-9).
Examples:
Input: str = ”123IN12345678.″
Output: TrueInput: str = ”IN12345678901″
Output: False
Explanation: As it starts with an alphabet letter.
Approach: The problem can be solved based on the following idea:
Create a regex pattern to validate the number as written below:
regex = “^[0-9]{3}[A-Z]{2}[0-9]{8}$“Where,
- ^ : Start of the string
- [0-9]{3}: This pattern will match 3 of the preceding items if they are digits(0-9)
- [A-Z]{2}: This pattern will match two of the preceding items if they are Uppercase Alphabet letters
- [0-9]{8}: This pattern will allow 8 of the preceding tokens if they are digits
- $: End of the string
Follow the below steps to implement the idea:
- Create a regex expression for Account Office Reference Number
- Use Pattern class to compile the regex formed.
- Use the matcher function to check whether the Account Office Reference Number is valid or not.
- If it is valid, return true. Otherwise, return false.
Below is the code implementation of the above-discussed approach:
Java
|
C++
|
Python3
|
C#
|
PHP
|
Javascript
|
true true false false false
Related Articles:
Given some Account Office Reference Number, the task is to check if they are valid or not using regular expressions. Rules for the valid Account Office Reference Number are:
- It is an alphanumeric string containing upper-case letters and digits.
- Accounts Office Reference Number is a unique, 13-character code.
- It starts with digits (0-9) and ends with digits.
- Its first three places are reserved for digits.
- After the first three characters, two places are occupied by uppercase alphabet letters (A-Z).
- After the first five characters, the next 8 places are reserved for digits (0-9).
Examples:
Input: str = ”123IN12345678.″
Output: TrueInput: str = ”IN12345678901″
Output: False
Explanation: As it starts with an alphabet letter.
Approach: The problem can be solved based on the following idea:
Create a regex pattern to validate the number as written below:
regex = “^[0-9]{3}[A-Z]{2}[0-9]{8}$“Where,
- ^ : Start of the string
- [0-9]{3}: This pattern will match 3 of the preceding items if they are digits(0-9)
- [A-Z]{2}: This pattern will match two of the preceding items if they are Uppercase Alphabet letters
- [0-9]{8}: This pattern will allow 8 of the preceding tokens if they are digits
- $: End of the string
Follow the below steps to implement the idea:
- Create a regex expression for Account Office Reference Number
- Use Pattern class to compile the regex formed.
- Use the matcher function to check whether the Account Office Reference Number is valid or not.
- If it is valid, return true. Otherwise, return false.
Below is the code implementation of the above-discussed approach:
Java
|
C++
|
Python3
|
C#
|
PHP
|
Javascript
|
true true false false false
Related Articles: