Match flight number using Regular Expression
Last Updated :
20 Dec, 2022
Given some Flight Numbers, the task is to check if they are valid or not using regular expressions. Rules for the valid flight numbers are:
- It is an alphanumeric string containing uppercase Alphabet letters(A-Z) and digits(0-9).
- It should always start with one of the alphabet letters and should end with always a digit.
- Its length may vary from 5 to 6 characters.
- If it contains whitespaces then by including this whitespace Its length should be between 6 to 7.
- It should not contain any special symbols.
Examples:
Input: str = “A12345”
Output: False
Explanation: First two characters should be from uppercase alphabet letters.
Input: str = “AI123456”
Output: False
Explanation: Its length is 8.
Input: str=”AI 123″
Output: True
Explanation: It follows the above noted points.
Approach: The problem can be solved based on the following idea:
Create a regex pattern to validate the number as written below:
regex= “^[A-Z]{2}\s{0, 1}[0-9]{3, 4}$”
Where,
^ : Start of the string
[A-Z]{2} : This pattern will match two of the preceding items in the range from “A” to “Z”.
\s{0, 1} : This pattern will match zero or one the preceding item if It is a whitespace.
[0-9]{3, 4} : This pattern will match 3 or 4 of the preceding items, if it is in the range from 0 to 9.
$: End of the String.
Follow the below steps to implement the idea:
- Create a regex expression for flight numbers.
- Use Pattern class to compile the regex formed.
- Use the matcher function to check whether the Flight Number is valid or not.
- If it is valid, return true. Otherwise, return false.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#include <regex>
using namespace std;
string isValid_FlightNumber(string str)
{
const regex pattern( "^[A-Z]{2}\\s{0,1}[0-9]{3,4}$" );
if (str.empty()) {
return "false" ;
}
if (regex_match(str, pattern)) {
return "true" ;
}
else {
return "false" ;
}
}
int main()
{
string str1 = "AI 123" ;
cout << isValid_FlightNumber(str1) << endl;
string str2 = "AI 1234" ;
cout << isValid_FlightNumber(str2) << endl;
string str3 = "AI1234" ;
cout << isValid_FlightNumber(str3) << endl;
string str4 = "AI 12AA" ;
cout << isValid_FlightNumber(str4) << endl;
string str5 = "AI123456" ;
cout << isValid_FlightNumber(str5) << endl;
string str6 = "A12345" ;
cout << isValid_FlightNumber(str6) << endl;
return 0;
}
|
Java
import java.util.regex.*;
class GFG {
public static boolean isValid_FlightNumber(String str)
{
String regex = "^[A-Z]{2}\\s{0, 1}[0-9]{3, 4}$" ;
Pattern p = Pattern.compile(regex);
if (str == null ) {
return false ;
}
Matcher m = p.matcher(str);
return m.matches();
}
public static void main(String args[])
{
String str1 = "AI 123" ;
System.out.println(isValid_FlightNumber(str1));
String str2 = "AI 1234" ;
System.out.println(isValid_FlightNumber(str2));
String str3 = "AI1234" ;
System.out.println(isValid_FlightNumber(str3));
String str4 = "AI 12AA" ;
System.out.println(isValid_FlightNumber(str4));
String str5 = "AI123456" ;
System.out.println(isValid_FlightNumber(str5));
String str6 = "A12345" ;
System.out.println(isValid_FlightNumber(str6));
}
}
|
Python3
import re
def isValid_FlightNumber( str ):
regex = "^[A-Z]{2}\s{0, 1}[0-9]{3, 4}$"
p = re. compile (regex)
if ( str = = None ):
return "false"
if (re.search(p, str )):
return "true"
else :
return "false"
if __name__ = = '__main__' :
str1 = "AI 123"
print (isValid_FlightNumber(str1))
str2 = "AI 1234"
print (isValid_FlightNumber(str2))
str3 = "AI1234"
print (isValid_FlightNumber(str3))
str4 = "AI 12AA"
print (isValid_FlightNumber(str4))
str5 = "AI123456"
print (isValid_FlightNumber(str5))
str6 = "A12345"
print (isValid_FlightNumber(str6))
|
C#
using System;
using System.Text.RegularExpressions;
public class GFG
{
public static bool isValid_FlightNumber(String str)
{
var regex = new Regex( "^[A-Z]{2}\\s{0, 1}[0-9]{3, 4}$" );
if (str == null )
{
return false ;
}
var m = regex.Match(str);
return m.Success;
}
public static void Main(String[] args)
{
var str1 = "AI 123" ;
Console.WriteLine(GFG.isValid_FlightNumber(str1));
var str2 = "AI 1234" ;
Console.WriteLine(GFG.isValid_FlightNumber(str2));
var str3 = "AI1234" ;
Console.WriteLine(GFG.isValid_FlightNumber(str3));
var str4 = "AI 12AA" ;
Console.WriteLine(GFG.isValid_FlightNumber(str4));
var str5 = "AI123456" ;
Console.WriteLine(GFG.isValid_FlightNumber(str5));
var str6 = "A12345" ;
Console.WriteLine(GFG.isValid_FlightNumber(str6));
}
}
|
Javascript
function isValid_FlightNumber(str) {
let regex = new RegExp(/^[A-Z]{2}\s{0, 1}[0-9]{3, 4}$/);
if (str == null ) {
return "false" ;
}
if (regex.test(str) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "AI 123" ;
console.log(isValid_FlightNumber(str1));
let str2 = "AI 1234" ;
console.log(isValid_FlightNumber(str2));
let str3 = "AI1234" ;
console.log(isValid_FlightNumber(str3));
let str4 = "AI 12AA" ;
console.log(isValid_FlightNumber(str4));
let str5 = "AI123456" ;
console.log(isValid_FlightNumber(str5));
let str6 = "A12345" ;
console.log(isValid_FlightNumber(str6));
|
Output
false
false
false
false
false
false
Time Complexity: O(N) for each test case, where N is the length of the given string.
Auxiliary Space: O(1)
Related Articles:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...