Regular Expressions to Validate Google Analytics Tracking Id

Improve Article

Save Article

Like Article

Improve Article

Save Article

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: True

Input: 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

import java.util.regex.*;

  

class GFG {

  

    

    

    public static boolean isValidGoogleAnaTrId(String str)

    {

  

        

        

        String regex

            = "^[A-Z]{2}[-]{0, 1}[0-9]{1, }[-]{0, 1}[0-9]{1, }$";

  

        

        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 = "AB-1234-45";

        System.out.println(isValidGoogleAnaTrId(str1));

  

        

        String str2 = "AB-1234-";

        System.out.println(isValidGoogleAnaTrId(str2));

  

        

        String str3 = "-AB-1234-";

        System.out.println(isValidGoogleAnaTrId(str3));

  

        

        String str4 = "AB-1234-1";

        System.out.println(isValidGoogleAnaTrId(str4));

    }

}

C++

#include <bits/stdc++.h>

#include <regex>

using namespace std;

  

string isValidGoogleAnaTrId(string str)

{

    

    

    const regex pattern(

        "^[A-Z]{2}[-]{0, 1}[0-9]{1, }[-]{0, 1}[0-9]{1, }$");

  

    

    

    if (str.empty()) {

        return "false";

    }

  

    

    

    if (regex_match(str, pattern)) {

        return "true";

    }

    else {

        return "false";

    }

}

  

int main()

{

    

    string str1 = "AB-1234-45";

    cout << isValidGoogleAnaTrId(str1) << endl;

  

    

    string str2 = "AB-1234-";

    cout << isValidGoogleAnaTrId(str2) << endl;

  

    

    string str3 = "-AB-1234-";

    cout << isValidGoogleAnaTrId(str3) << endl;

  

    

    string str4 = "AB-1234-1";

    cout << isValidGoogleAnaTrId(str4) << endl;

  

    return 0;

}

Python3

  

import re

  

  

def isValidGoogleAnaTrId(str):

  

    

    regex = "^[A-Z]{2}[-]{0, 1}[0-9]{1, }[-]{0, 1}[0-9]{1, }$"

  

    

    p = re.compile(regex)

  

    

    

    if (str == None):

        return "false"

  

    

    

    if(re.search(p, str)):

        return "true"

    else:

        return "false"

  

  

if __name__ == '__main__':

  

    

    str1 = "AB-1234-45"

    print(isValidGoogleAnaTrId(str1))

  

    

    str2 = "AB-1234-"

    print(isValidGoogleAnaTrId(str2))

  

    

    str3 = "-AB-1234-"

    print(isValidGoogleAnaTrId(str3))

  

    

    str4 = "AB-1234-1"

    print(isValidGoogleAnaTrId(str4))

C#

using System;

using System.Text.RegularExpressions;

class GFG {

  

    

    static void Main(string[] args)

    {

  

        

        

        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();

    }

  

    

    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

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

  

function isValidGoogleAnaTrId(str) {

    

    

    let regex = new RegExp(/^[A-Z]{2}[-]{0, 1}[0-9]{1, }[-]{0, 1}[0-9]{1, }$/);

  

    

    

    if (str == null) {

        return "false";

    }

  

    

    

    if (regex.test(str) == true) {

        return "true";

    }

    else {

        return "false";

    }

}

  

let str1 = "AB-1234-45";

console.log(isValidGoogleAnaTrId(str1));

  

let str2 = "AB-1234-";

console.log(isValidGoogleAnaTrId(str2));

  

let str3 = "-AB-1234-";

console.log(isValidGoogleAnaTrId(str3));

  

let str4 = "AB-1234-1";

console.log(isValidGoogleAnaTrId(str4));

Output
true
false
false
true

Related Articles:

Like this post? Please share to your friends:
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: