Open In App

Match flight number using Regular Expression

Last Updated : 20 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ program to validate the
// Flight Number using Regular
// Expression
 
#include <bits/stdc++.h>
#include <regex>
using namespace std;
 
// Function to validate the
// Flight Number
string isValid_FlightNumber(string str)
{
    // Regex to check valid
    // Flight Number .
    const regex pattern("^[A-Z]{2}\\s{0,1}[0-9]{3,4}$");
 
    // If the str
    // is empty return false
    if (str.empty()) {
        return "false";
    }
 
    // Return true if the str
    // matched the ReGex
    if (regex_match(str, pattern)) {
        return "true";
    }
    else {
        return "false";
    }
}
 
// Driver Code
int main()
{
    // Test Case 1:
    string str1 = "AI 123";
    cout << isValid_FlightNumber(str1) << endl;
 
    // Test Case 2:
    string str2 = "AI 1234";
    cout << isValid_FlightNumber(str2) << endl;
 
    // Test Case 3:
    string str3 = "AI1234";
    cout << isValid_FlightNumber(str3) << endl;
 
    // Test Case 4:
    string str4 = "AI 12AA";
    cout << isValid_FlightNumber(str4) << endl;
 
    // Test Case 5:
    string str5 = "AI123456";
    cout << isValid_FlightNumber(str5) << endl;
 
    // Test Case 6:
    string str6 = "A12345";
    cout << isValid_FlightNumber(str6) << endl;
 
    return 0;
}


Java




// Java program to validate the
// Flight Number using Regular Expression
 
import java.util.regex.*;
 
class GFG {
 
    // Function to validate the
    // Flight Number
    public static boolean isValid_FlightNumber(String str)
    {
        // Regex to check valid Flight Number
        String regex = "^[A-Z]{2}\\s{0, 1}[0-9]{3, 4}$";
 
        // Compile the ReGex
        Pattern p = Pattern.compile(regex);
 
        // If the str
        // is empty return false
        if (str == null) {
            return false;
        }
 
        // Pattern class contains matcher()
        // method to find matching between
        // given str using regex.
        Matcher m = p.matcher(str);
 
        // Return if the str
        // matched the ReGex
        return m.matches();
    }
 
    // Driver Code.
    public static void main(String args[])
    {
 
        // Test Case 1:
        String str1 = "AI 123";
        System.out.println(isValid_FlightNumber(str1));
 
        // Test Case 2:
        String str2 = "AI 1234";
        System.out.println(isValid_FlightNumber(str2));
 
        // Test Case 3:
        String str3 = "AI1234";
        System.out.println(isValid_FlightNumber(str3));
 
        // Test Case 4:
        String str4 = "AI 12AA";
        System.out.println(isValid_FlightNumber(str4));
 
        // Test Case 5:
        String str5 = "AI123456";
        System.out.println(isValid_FlightNumber(str5));
 
        // Test Case 6:
        String str6 = "A12345";
        System.out.println(isValid_FlightNumber(str6));
    }
}


Python3




# Python3 program to validate
# Flight Number using Regular Expression
 
import re
 
 
# Function to validate
# Flight Number
def isValid_FlightNumber(str):
 
    # Regex to check valid Flight Number
    regex = "^[A-Z]{2}\s{0, 1}[0-9]{3, 4}$"
 
    # Compile the ReGex
    p = re.compile(regex)
 
    # If the string is empty
    # return false
    if (str == None):
        return "false"
 
    # Return if the string
    # matched the ReGex
    if(re.search(p, str)):
        return "true"
    else:
        return "false"
 
 
# Driver code
if __name__ == '__main__':
     
    # Test Case 1:
    str1 = "AI 123"
    print(isValid_FlightNumber(str1))
     
    # Test Case 2:
    str2 = "AI 1234"
    print(isValid_FlightNumber(str2))
     
    # Test Case 3:
    str3 = "AI1234"
    print(isValid_FlightNumber(str3))
     
    # Test Case 4:
    str4 = "AI 12AA"
    print(isValid_FlightNumber(str4))
     
    # Test Case 5:
    str5 = "AI123456"
    print(isValid_FlightNumber(str5))
     
    # Test Case 6:
    str6 = "A12345"
    print(isValid_FlightNumber(str6))


C#




// Include namespace system
using System;
using System.Text.RegularExpressions;
 
public class GFG
{
 
  // Function to validate the
  // Flight Number
  public static bool isValid_FlightNumber(String str)
  {
 
    // Regex to check valid Flight Number
    var regex = new Regex("^[A-Z]{2}\\s{0, 1}[0-9]{3, 4}$");
 
    // If the str
    // is empty return false
    if (str == null)
    {
      return false;
    }
    // Pattern class contains matcher()
    // method to find matching between
    // given str using regex.
    var m = regex.Match(str);
    // Return if the str
    // matched the ReGex
    return m.Success;
  }
  // Driver Code.
  public static void Main(String[] args)
  {
    // Test Case 1:
    var str1 = "AI 123";
    Console.WriteLine(GFG.isValid_FlightNumber(str1));
 
    // Test Case 2:
    var str2 = "AI 1234";
    Console.WriteLine(GFG.isValid_FlightNumber(str2));
 
    // Test Case 3:
    var str3 = "AI1234";
    Console.WriteLine(GFG.isValid_FlightNumber(str3));
 
    // Test Case 4:
    var str4 = "AI 12AA";
    Console.WriteLine(GFG.isValid_FlightNumber(str4));
 
    // Test Case 5:
    var str5 = "AI123456";
    Console.WriteLine(GFG.isValid_FlightNumber(str5));
 
    // Test Case 6:
    var str6 = "A12345";
    Console.WriteLine(GFG.isValid_FlightNumber(str6));
  }
}
 
// This code is contributed by aadityaburujwale.


Javascript




// Javascript program to validate
// flight Number using Regular Expression
 
// Function to validate the
// Flight Number
function isValid_FlightNumber(str) {
    // Regex to check valid
    // Flight Number
    let regex = new RegExp(/^[A-Z]{2}\s{0, 1}[0-9]{3, 4}$/);
 
    // If str
    // is empty return false
    if (str == null) {
        return "false";
    }
 
    // Return true if the str
    // matched the ReGex
    if (regex.test(str) == true) {
        return "true";
    }
    else {
        return "false";
    }
}
 
// Driver Code
// Test Case 1:
let str1 = "AI 123";
console.log(isValid_FlightNumber(str1));
 
// Test Case 2:
let str2 = "AI 1234";
console.log(isValid_FlightNumber(str2));
 
// Test Case 3:
let str3 = "AI1234";
console.log(isValid_FlightNumber(str3));
 
// Test Case 4:
let str4 = "AI 12AA";
console.log(isValid_FlightNumber(str4));
 
// Test Case 5:
let str5 = "AI123456";
console.log(isValid_FlightNumber(str5));
 
// Test Case 6:
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
Previous
Next
Share your thoughts in the comments

Similar Reads