Given some Google Analytics Tracking IDs, the task is to check if they are valid or not using regular expressions. Rules for the valid Tracking Id are:
- It is an alphanumeric string i.e., containing digits (0-9), alphabets (A-Z), and a Special character hyphen(-).
- The hyphen will come in between the given Google Analytics Tracking Id.
- Google Analytics Tracking Id should not start and end with a hyphen (-).
- It should not contains whitespaces and other special characters rather than a hyphen symbol.
Examples:
Input: str = ”AB-1234-45”
Output: TrueInput: str = ”AB-1234-”
Output: False
Explanation: Google Analytics Tracking Id never ends with a hyphen.
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}[-]{0, 1}[0-9]{1, }[-]{0, 1}[0-9]{1, }$“Where,
- ^: Indiactes the start of the string
- [A-Z]{2}: This pattern will allow two of the preceding items if they are uppercase alphabets (A-Z).
- [-]{0, 1}: This pattern will match zero or one of the preceding items if it is a hyphen symbol.
- [0-9]{1}: This pattern will allow one or more than one preceding items if it is a digit.
- $ : Indicates the end of the string.
Follow the below steps to implement the idea:
- Create a regex expression forTracking Id.
- Use Pattern class to compile the regex formed.
- Use the matcher function to check whether the Tracking id is valid or not.
- If it is valid, return true. Otherwise, return false.
Below is the code implementation of the above-discussed approach:
Java
// Java program to validate the // Google Analytics Tracking Id // using Regular Expression import java.util.regex.*; class GFG { // Function to validate the // Google Analytics Tracking Id public static boolean isValidGoogleAnaTrId(String str) { // Regex to check valid Google // Analytics Tracking Id String regex = "^[A-Z]{2}[-]{0, 1}[0-9]{1, }[-]{0, 1}[0-9]{1, }$" ; // 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 Code using regex. Matcher m = p.matcher(str); // Return if the str Code // matched the ReGex return m.matches(); } // Driver Code. public static void main(String args[]) { // Test Case 1: String str1 = "AB-1234-45" ; System.out.println(isValidGoogleAnaTrId(str1)); // Test Case 2: String str2 = "AB-1234-" ; System.out.println(isValidGoogleAnaTrId(str2)); // Test Case 3: String str3 = "-AB-1234-" ; System.out.println(isValidGoogleAnaTrId(str3)); // Test Case 4: String str4 = "AB-1234-1" ; System.out.println(isValidGoogleAnaTrId(str4)); } } |
C++
// C++ program to validate the // Google Analytics Tracking Id // using Regular Expression #include <bits/stdc++.h> #include <regex> using namespace std; // Function to validate the // Google Analytics Tracking Id string isValidGoogleAnaTrId(string str) { // Regex to check valid // Google Analytics Tracking Id const regex pattern( "^[A-Z]{2}[-]{0, 1}[0-9]{1, }[-]{0, 1}[0-9]{1, }$" ); // 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 = "AB-1234-45" ; cout << isValidGoogleAnaTrId(str1) << endl; // Test Case 2: string str2 = "AB-1234-" ; cout << isValidGoogleAnaTrId(str2) << endl; // Test Case 3: string str3 = "-AB-1234-" ; cout << isValidGoogleAnaTrId(str3) << endl; // Test Case 4: string str4 = "AB-1234-1" ; cout << isValidGoogleAnaTrId(str4) << endl; return 0; } |
Python3
# Python3 program to validate # Google Analytics Tracking Id using Regular # Expression import re # Function to validate # Google Analytics Tracking Id def isValidGoogleAnaTrId( str ): # Regex to check valid Google Analytics Tracking Id regex = "^[A-Z]{2}[-]{0, 1}[0-9]{1, }[-]{0, 1}[0-9]{1, }$" # 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 = "AB-1234-45" print (isValidGoogleAnaTrId(str1)) # Test Case 2: str2 = "AB-1234-" print (isValidGoogleAnaTrId(str2)) # Test Case 3: str3 = "-AB-1234-" print (isValidGoogleAnaTrId(str3)) # Test Case 4: str4 = "AB-1234-1" print (isValidGoogleAnaTrId(str4)) |
C#
// C# program to validate the // Google Analytics Tracking Id // using Regular Expressions using System; using System.Text.RegularExpressions; class GFG { // Main Method static void Main( string [] args) { // Input strings to Match // Google Analytics Tracking Id string [] str = { "AB-1234-45" , "AB-1234-" , "-AB-1234-" , "AB-1234-1" }; foreach ( string s in str) { Console.WriteLine( isValidGoogleAnaTrId(s) ? "true" : "false" ); } Console.ReadKey(); } // method containing the regex public static bool isValidGoogleAnaTrId( string str) { string strRegex = @"^[A-Z]{2}[-]{0, 1}[0-9]{1, }[-]{0, 1}[0-9]{1, }$" ; Regex re = new Regex(strRegex); if (re.IsMatch(str)) return ( true ); else return ( false ); } } |
PHP
<?php // PHP program to validate Google Analytics Tracking Id // Function to validate Google Analytics Tracking Id using regular expression function isValidGoogleAnaTrId( $str ){ if (preg_match( '/^[A-Z]{2}[-]{0, 1}[0-9]{1, }[-]{0, 1}[0-9]{1, }$/' , $str )) { echo "true\n" ; } else { echo "false\n" ; } } isValidGoogleAnaTrId( "AB-1234-45" ); isValidGoogleAnaTrId( "AB-1234-" ); isValidGoogleAnaTrId( "-AB-1234-" ); isValidGoogleAnaTrId( "AB-1234-1" ); ?> |
Javascript
// Javascript program to validate // Google Analytics Tracking Id using Regular Expression // Function to validate the // Google Analytics Tracking Id function isValidGoogleAnaTrId(str) { // Regex to check valid // Google Analytics Tracking Id let regex = new RegExp(/^[A-Z]{2}[-]{0, 1}[0-9]{1, }[-]{0, 1}[0-9]{1, }$/); // 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 = "AB-1234-45" ; console.log(isValidGoogleAnaTrId(str1)); // Test Case 2: let str2 = "AB-1234-" ; console.log(isValidGoogleAnaTrId(str2)); // Test Case 3: let str3 = "-AB-1234-" ; console.log(isValidGoogleAnaTrId(str3)); // Test Case 4: let str4 = "AB-1234-1" ; console.log(isValidGoogleAnaTrId(str4)); |
true false false true
Time Complexity: O(N) for each testcase, where N is the length of the given string.
Auxiliary Space: O(1)
Related Articles:
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!