Given some Google Analytics Monitoring IDs, the duty is to verify if they’re legitimate or not utilizing common expressions. Guidelines for the legitimate Monitoring Id are:
- It’s an alphanumeric string i.e., containing digits (0-9), alphabets (A-Z), and a Particular character hyphen(-).
- The hyphen will are available between the given Google Analytics Monitoring Id.
- Google Analytics Monitoring Id shouldn’t begin and finish with a hyphen (-).
- It shouldn’t incorporates whitespaces and different particular characters slightly than a hyphen image.
Examples:
Enter: str = ”AB-1234-45”
Output: TrueEnter: str = ”AB-1234-”
Output: False
Clarification: Google Analytics Monitoring Id by no means ends with a hyphen.
Strategy: The issue may be solved based mostly on the next thought:
Create a regex sample to validate the quantity as written under:
regex = “^[A-Z]{2}[-]{0, 1}[0-9]{1, }[-]{0, 1}[0-9]{1, }$“The place,
- ^: Indiactes the beginning of the string
- [A-Z]{2}: This sample will permit two of the previous objects if they’re uppercase alphabets (A-Z).
- [-]{0, 1}: This sample will match zero or one of many previous objects if it’s a hyphen image.
- [0-9]{1}: This sample will permit one or a couple of previous objects if it’s a digit.
- $ : Signifies the top of the string.
Observe the under steps to implement the concept:
- Create a regex expression forTracking Id.
- Use Sample class to compile the regex shaped.
- Use the matcher operate to verify whether or not the Monitoring id is legitimate or not.
- Whether it is legitimate, return true. In any other case, return false.
Under is the code implementation of the above-discussed strategy:
Java
// Java program to validate the // Google Analytics Monitoring Id // utilizing Common Expression import java.util.regex.*; class GFG { // Perform to validate the // Google Analytics Monitoring Id public static boolean isValidGoogleAnaTrId(String str) { // Regex to verify legitimate Google // Analytics Monitoring Id String regex = "^[A-Z]{2}[-]{0, 1}[0-9]{1, }[-]{0, 1}[0-9]{1, }$"; // Compile the Regex Sample p = Sample.compile(regex); // If the str // is empty return false if (str == null) { return false; } // Sample class incorporates matcher() // methodology to search out matching between // given str Code utilizing regex. Matcher m = p.matcher(str); // Return if the str Code // matched the ReGex return m.matches(); } // Driver Code. public static void predominant(String args[]) { // Check Case 1: String str1 = "AB-1234-45"; System.out.println(isValidGoogleAnaTrId(str1)); // Check Case 2: String str2 = "AB-1234-"; System.out.println(isValidGoogleAnaTrId(str2)); // Check Case 3: String str3 = "-AB-1234-"; System.out.println(isValidGoogleAnaTrId(str3)); // Check Case 4: String str4 = "AB-1234-1"; System.out.println(isValidGoogleAnaTrId(str4)); } }
C++
// C++ program to validate the // Google Analytics Monitoring Id // utilizing Common Expression #embody <bits/stdc++.h> #embody <regex> utilizing namespace std; // Perform to validate the // Google Analytics Monitoring Id string isValidGoogleAnaTrId(string str) { // Regex to verify legitimate // Google Analytics Monitoring Id const regex sample( "^[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, sample)) { return "true"; } else { return "false"; } } // Driver Code int predominant() { // Check Case 1: string str1 = "AB-1234-45"; cout << isValidGoogleAnaTrId(str1) << endl; // Check Case 2: string str2 = "AB-1234-"; cout << isValidGoogleAnaTrId(str2) << endl; // Check Case 3: string str3 = "-AB-1234-"; cout << isValidGoogleAnaTrId(str3) << endl; // Check Case 4: string str4 = "AB-1234-1"; cout << isValidGoogleAnaTrId(str4) << endl; return 0; }
Python3
# Python3 program to validate # Google Analytics Monitoring Id utilizing Common # Expression import re # Perform to validate # Google Analytics Monitoring Id def isValidGoogleAnaTrId(str): # Regex to verify legitimate Google Analytics Monitoring 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__': # Check Case 1: str1 = "AB-1234-45" print(isValidGoogleAnaTrId(str1)) # Check Case 2: str2 = "AB-1234-" print(isValidGoogleAnaTrId(str2)) # Check Case 3: str3 = "-AB-1234-" print(isValidGoogleAnaTrId(str3)) # Check Case 4: str4 = "AB-1234-1" print(isValidGoogleAnaTrId(str4))
C#
// C# program to validate the // Google Analytics Monitoring Id // utilizing Common Expressions utilizing System; utilizing System.Textual content.RegularExpressions; class GFG { // Essential Technique static void Essential(string[] args) { // Enter strings to Match // Google Analytics Monitoring 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(); } // methodology 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 Monitoring Id // Perform to validate Google Analytics Monitoring Id utilizing common expression operate isValidGoogleAnaTrId($str){ if(preg_match('/^[A-Z]{2}[-]{0, 1}[0-9]{1, }[-]{0, 1}[0-9]{1, }$/', $str)) { echo "truen"; } else { echo "falsen"; } } isValidGoogleAnaTrId("AB-1234-45"); isValidGoogleAnaTrId("AB-1234-"); isValidGoogleAnaTrId("-AB-1234-"); isValidGoogleAnaTrId("AB-1234-1"); ?>
Javascript
// Javascript program to validate // Google Analytics Monitoring Id utilizing Common Expression // Perform to validate the // Google Analytics Monitoring Id operate isValidGoogleAnaTrId(str) { // Regex to verify legitimate // Google Analytics Monitoring 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.check(str) == true) { return "true"; } else { return "false"; } } // Driver Code // Check Case 1: let str1 = "AB-1234-45"; console.log(isValidGoogleAnaTrId(str1)); // Check Case 2: let str2 = "AB-1234-"; console.log(isValidGoogleAnaTrId(str2)); // Check Case 3: let str3 = "-AB-1234-"; console.log(isValidGoogleAnaTrId(str3)); // Check Case 4: let str4 = "AB-1234-1"; console.log(isValidGoogleAnaTrId(str4));
true false false true
Associated Articles: