0% found this document useful (0 votes)
7 views11 pages

RE Programs

Uploaded by

PTJH Productions
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views11 pages

RE Programs

Uploaded by

PTJH Productions
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

How to validate MAC address using

Regular Expression
Given string str, the task is to check whether the given string is a valid MAC
address or not by using Regular Expression.

Media Access Control (MAC) Address –

MAC Addresses are unique 48-bits hardware number of a computer, which is


embedded into a network card (known as a Network Interface Card) during the
time of manufacturing. MAC Address is also known as the Physical Address of
a network device

A valid MAC address must satisfy the following conditions:


1. It must contain 12 hexadecimal digits.
2. One way to represent them is to form six pairs of the characters separated
with a hyphen (-) or colon(:). For example, 01-23-45-67-89-AB is a valid MAC
address.
3. Another way to represent them is to form three groups of four hexadecimal
digits separated by dots(.). For example, 0123.4567.89AB is a valid MAC
address.
Examples:
Input: str = “01-23-45-67-89-AB”;
Output: true
Explanation:
The given string satisfies all the above mentioned conditions. Therefore, it is a valid
MAC address.
Input: str = “01-23-45-67-89-AH”;
Output: false
Explanation:
The given string contains ‘H’, the valid hexadecimal digits should be followed by letter
from a-f, A-F, and 0-9. Therefore, it is not a valid MAC address.
Input: str = “01-23-45-67-AH”;
Output: false
Explanation:
The given string has five groups of two hexadecimal digits. Therefore, it is not a valid
MAC address.

Approach: The idea is to use Regular Expression to solve this problem. The
following steps can be followed to compute the answer.
• Get the String.
• Create a regular expression to check valid MAC address as mentioned
below:
regex = “^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})|([0-9a-fA-F]{4}\\.[0-9a-fA-F]{4}\\.[0-
9a-fA-F]{4})$”;
• Where:
• ^ represents the starting of the string.
• ([0-9A-Fa-f]{2}[:-]){5} represents the five groups of two
hexadecimal digits separated by hyphens (-) or colons (:)
• ([0-9A-Fa-f]{2}) represents the one groups of two hexadecimal
digits.
• | represents the or.
• ( represents the starting of the group.
• [0-9a-fA-F]{4}\\. represents the first part of four hexadecimal
digits separated by dots (.).
• [0-9a-fA-F]{4}\\. represents the second part of four
hexadecimal digits separated by dots (.).
• [0-9a-fA-F]{4} represents the third part of four hexadecimal
digits.
• ) represents the ending of the group.
• $ represents the ending of the string.
• Match the given string with the Regular Expression. In Java, this can be
done by using Pattern.matcher().
• Return true if the string matches with the given regular expression, else
return false.
Below is the implementation of the above approach:

// Java program to validate MAC address using regular expression

import java.util.regex.*;
class GFG {

// Function to validate
// MAC address
// using regular expression
public static boolean isValidMACAddress(String str)
{
// Regex to check valid
// MAC address
String regex = "^([0-9A-Fa-f]{2}[:-])"
+ "{5}([0-9A-Fa-f]{2})|"
+ "([0-9a-fA-F]{4}\\."
+ "[0-9a-fA-F]{4}\\."
+ "[0-9a-fA-F]{4})$";

// Compile the ReGex


Pattern p = Pattern.compile(regex);

// If the string is empty


// return false
if (str == null)
{
return false;
}

// Find match between given string


// and regular expression
// uSing Pattern.matcher()

Matcher m = p.matcher(str);

// Return if the string


// matched the ReGex
return m.matches();
}

// Driver code
public static void main(String args[])
{

// Test Case 1:
String str1 = "01-23-45-67-89-AB";
System.out.println(isValidMACAddress(str1));

// Test Case 2:
String str2 = "01:23:45:67:89:AB";
System.out.println(isValidMACAddress(str2));

// Test Case 3:
String str3 = "0123.4567.89AB";
System.out.println(isValidMACAddress(str3));

// Test Case 4:
String str4 = "01-23-45-67-89-AH";
System.out.println(isValidMACAddress(str4));

// Test Case 5:
String str5 = "01-23-45-67-AH";
System.out.println(isValidMACAddress(str5));
}
}

Output
true
true
true
false
false

How to validate Indian driving license


number using Regular Expression
Given string str, the task is to check whether the given string is a valid Indian driving
license number or not by using Regular Expression.

The valid Indian driving license number must satisfy the following conditions:
1. It should be 16 characters long (including space or hyphen (-)).
2. The driving license number can be entered in any of the following formats:
HR-0619850034761
OR
HR06 19850034761
1. The first two characters should be upper-case alphabets that represent the
state code.
2. The next two characters should be digits that represent the RTO code.
3. The next four characters should be digits that represent the license issued
in a year.
4. The next seven characters should be any digits from 0-9.
Note: In this article, we will check the license issued year from 1900-2099. It can be
customized to change the license issued year.
Examples:
Input: str = “HR-0619850034761”;
Output: true
Explanation:
The given string satisfies all the above mentioned conditions. Therefore, it is not a valid
Indian driving license number.
Input: str = “MH27 30120034761”;
Output: false
Explanation:
The given string has the license issued year 3012, that is not a valid year because in
this article we validate the year from 1900-2099. Therefore, it is not a valid Indian
driving license number.
Input: str = “GJ-2420180”;
Output: false
Explanation:
The given string has 10 characters. Therefore, it is not a valid Indian driving license
number.
Approach: The idea is to use Regular Expression to solve this problem. The
following steps can be followed to compute the answer:
• Get the String.
• Create a regular expression to check valid Indian driving license numbers
as mentioned below:
regex = “^(([A-Z]{2}[0-9]{2})( )|([A-Z]{2}-[0-9]{2}))((19|20)[0-9][0-9])[0-9]{7}$”;
• Where:
• ^ represents the starting of the string.
• ( represents the starting of group 1.
• ( represents the starting of group 2.
• [A-Z]{2} represents the first two characters should be upper
case alphabets.
• [0-9]{2} represents the next two characters should be digits.
• ) represents the ending of group 2.
• ( ) represents the white space character.
• | represents the or.
• ( represents the starting of group 3.
• [A-Z]{2} represents the first two characters should be upper
case alphabets.
• – represents the hyphen.
• [0-9]{2} represents the next two characters should be digits.
• ) represents the ending of the group 3.
• ) represents the ending of the group 1.
• ((19|20)[0-9][0-9]) represents the year from 1900-2099.
• [0-9]{7} represents the next seven characters should be any
digits from 0-9.
• $ represents the ending of the string.
• Match the given string with the Regular Expression, In Java, this can be
done by using Pattern.matcher().
• Return true if the string matches with the given regular expression, else
return false.
Below is the implementation of the above approach:

// Java program to validate Indian driving license number using


regular expression

import java.util.regex.*;
class GFG {

// Function to validate
// Indian driving license number
// using regular expression
public static boolean isValidLicenseNo(String str)
{
// Regex to check valid
// Indian driving license number
String regex = "^(([A-Z]{2}[0-9]{2})"
+ "( )|([A-Z]{2}-[0-9]"
+ "{2}))((19|20)[0-9]"
+ "[0-9])[0-9]{7}$";

// Compile the ReGex


Pattern p = Pattern.compile(regex);

// If the string is empty


// return false
if (str == null) {
return false;
}

// Find match between given string


// and regular expression
// uSing Pattern.matcher()

Matcher m = p.matcher(str);

// Return if the string


// matched the ReGex
return m.matches();
}
// Driver code
public static void main(String args[])
{

// Test Case 1:
String str1 = "HR-0619850034761";
System.out.println(isValidLicenseNo(str1));

// Test Case 2:
String str2 = "UP14 20160034761";
System.out.println(isValidLicenseNo(str2));

// Test Case 3:
String str3 = "12HR-37200602347";
System.out.println(isValidLicenseNo(str3));

// Test Case 4:
String str4 = "MH27 30123476102";
System.out.println(isValidLicenseNo(str4));

// Test Case 5:
String str5 = "GJ-2420180";
System.out.println(isValidLicenseNo(str5));
}
}

Output
true
true
false
false
false

Check whether two convex regular


polygon have same center or not
Given two positive integers N and M which denotes the sides of the convex regular
polygon where N < M, the task is to check whether polygons have the same center or
not if N-sided polygon was inscribed in an M-sided polygon.
Center of Polygon: Point inside a polygon which is equidistant from each vertex of
the polygon.
Examples:

Input: N = 9, M = 3
Output: YES
Explanation:
Polygon of side 3 when inscribed in a polygon of side 9, then both polygons have same
center.
Input: N = 10, M = 3
Output: NO
Explanation:
Polygon of side 3 when inscribed in a polygon of side 10, then both polygons don’t have
same center.

Approach: The key observation in this problem is that when M % N == 0, that


means the sides of N-sided polygon equally covers the sides of M-sided polygon,
which means both the polygons have same center.
Algorithm:

• Check if M is divisible by N, If yes then both the polygons have same


center.
• Otherwise both polygons have the different centers.
Below is the implementation of the above approach:

//Java implementation to check whether two convex polygons have same


center
class GFG{

// Function to check whether two convex


// polygons have the same center or not
static int check(int n, int m){
if (m % n == 0){
System.out.print("YES");
}
else{
System.out.print("NO");
}
return 0;
}
// Driver Code
public static void main(String[] args)
{
int n = 5;
int m = 10;

check(n, m);
}
}

Output:
YES

Check if an URL is valid or not using


Regular Expression
• Difficulty Level : Medium
• Last Updated : 11 Feb, 2021

• Read

• Discuss
Given a URL as a character string str of size N.The task is to check if the given URL is
valid or not.
Examples :
Input : str = “https://www.geeksforgeeks.org/”
Output : Yes
Explanation :
The above URL is a valid URL.
Input : str = “https:// www.geeksforgeeks.org/”
Output : No
Explanation :
Note that there is a space after https://, hence the URL is invalid.

Approach :
An approach using java.net.url class to validate a URL is discussed in
the previous post.
Here the idea is to use Regular Expression to validate a URL.
• Get the URL.
• Create a regular expression to check the valid URL as mentioned below:
regex = “((http|https)://)(www.)?”
+ “[a-zA-Z0-9@:%._\\+~#?&//=]{2,256}\\.[a-z]”
+ “{2,6}\\b([-a-zA-Z0-9@:%._\\+~#?&//=]*)”

• The URL must start with either http or https and


• then followed by :// and
• then it must contain www. and
• then followed by subdomain of length (2, 256) and
• last part contains top level domain like .com, .org etc.
• Match the given URL with the regular expression. In Java, this can be done
by using Pattern.matcher().
• Return true if the URL matches with the given regular expression, else
return false.
Below is the implementation of the above approach:

// Java program to check URL is valid or not using Regular


expression

import java.util.regex.*;

class GFG {

// Function to validate URL


// using regular expression
public static boolean
isValidURL(String url)
{
// Regex to check valid URL
String regex = "((http|https)://)(www.)?"
+ "[a-zA-Z0-9@:%._\\+~#?&//=]"
+ "{2,256}\\.[a-z]"
+ "{2,6}\\b([-a-zA-Z0-9@:%"
+ "._\\+~#?&//=]*)";

// Compile the ReGex


Pattern p = Pattern.compile(regex);

// If the string is empty


// return false
if (url == null) {
return false;
}

// Find match between given string


// and regular expression
// using Pattern.matcher()
Matcher m = p.matcher(url);

// Return if the string


// matched the ReGex
return m.matches();
}

// Driver code
public static void main(String args[])
{
String url
= "https://www.geeksforgeeks.org";
if (isValidURL(url) == true) {
System.out.println("Yes");
}
else
System.out.println("NO");
}
}

Output:
Yes

You might also like