Given some words of Gender, the task is to check if they are valid or not using regular expressions. The correct responses can be as given below:
- Male / male / MALE / M / m
- Female / female / FEMALE / F / f
- Not prefer to say
Example:
Input: M
Output: TrueInput: S
Output: False
Approach: The problem can be solved based on the following idea:
Create a regex pattern to validate the number as written below:
regex= “(?:m|M|male|Male|f|F|female|Female|FEMALE|MALE|Not prefer to say)$”
Just check whether any of the words are present in regex or not using (?:)
Follow the below steps to implement the idea:
- Create a regular expression for Gender.
- If the Input string is empty, return False.
- Else, Use the Pattern class to compile the regex formed.
- Use the matcher function to check whether the Gender Is valid or not.
- If it is valid, return true. Otherwise, return false.
Below is the Implementation of the above approach:
// C++ program to validate the GENDER // using Regular Expression #include <bits/stdc++.h> #include <regex> using namespace std;
// Function to validate the GENDER string isValidGender(string str) { // Regex to check valid GENDER .
const regex pattern(
"(?:m|M|male|Male|f|F|female|Female|FEMALE|MALE|"
"Not prefer to say)$" );
// If the GENDER is empty return false
if (str.empty()) {
return "false" ;
}
// Return true if the GENDER
// matched the ReGex
if (regex_match(str, pattern)) {
return "true" ;
}
else {
return "false" ;
}
} // Driver Code int main()
{ // Test Case 1:
string str1 = "m" ;
cout << isValidGender(str1) << endl;
// Test Case 2:
string str2 = "f" ;
cout << isValidGender(str2) << endl;
// Test Case 3:
string str3 = "F" ;
cout << isValidGender(str3) << endl;
// Test Case 4:
string str4 = "M" ;
cout << isValidGender(str4) << endl;
// Test Case 5:
string str5 = "MALE" ;
cout << isValidGender(str5) << endl;
// Test Case 6:
string str6 = "S" ;
cout << isValidGender(str6) << endl;
// Test case 7:
string str7 = "Not prefer to say" ;
cout << isValidGender(str7) << endl;
return 0;
} |
// Java program to validate the GENDER // using Regular Expression import java.io.*;
import java.util.regex.*;
class GFG {
// Function to validate the GENDER
public static boolean isValidGender(String str)
{
// Regex to check valid GENDER
String regex
= "(?:m|M|male|Male|f|F|female|Female|FEMALE|MALE|Not prefer to say)$" ;
// Compile the ReGex
Pattern p = Pattern.compile(regex);
// If the GENDER is empty return false
if (str == null ) {
return false ;
}
// Pattern class contains matcher()
// method to find matching between
// given GENDER 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 = "m" ;
System.out.println(isValidGender(str1));
// Test Case 2:
String str2 = "f" ;
System.out.println(isValidGender(str2));
// Test Case 3:
String str3 = "F" ;
System.out.println(isValidGender(str3));
// Test Case 4:
String str4 = "M" ;
System.out.println(isValidGender(str4));
// Test Case 5:
String str5 = "FEMALE" ;
System.out.println(isValidGender(str5));
// Test Case 6:
String str6 = "S" ;
System.out.println(isValidGender(str6));
// Test case 7:
String str7 = "Not prefer to say" ;
System.out.println(isValidGender(str7));
}
} |
# Python3 program to validate GENDER # using Regular Expression import re
# Function to validate GENDER def isValidGender( str ):
# Regex to check valid GENDER
regex = "(?:m|M|male|Male|f|F|female|Female|FEMALE|MALE|Not prefer to say)$"
# 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 = "m"
print (isValidGender(str1))
# Test Case 2:
str2 = "f"
print (isValidGender(str2))
# Test Case 3:
str3 = "F"
print (isValidGender(str3))
# Test Case 4:
str4 = "M"
print (isValidGender(str4))
# Test Case 5:
str5 = "FEMALE"
print (isValidGender(str5))
# Test Case 6:
str6 = "S"
print (isValidGender(str6))
# Test Case 7:
str7 = "Not prefer to say"
print (isValidGender(str7))
|
// C# program to validate the GENDER // using Regular Expression using System;
using System.Text.RegularExpressions;
public class GFG {
// Function to validate the GENDER
public static bool isValidGender( string str)
{
// Regex to check valid GENDER
string regex
= "(?:m|M|male|Male|f|F|female|Female|FEMALE|MALE|Not prefer to say)$" ;
// Compile the ReGex
Regex p = new Regex(regex);
// If the GENDER is empty return false
if (str == null ) {
return false ;
}
// Pattern class contains matcher()
// method to find matching between
// given GENDER using regex.
Match m = p.Match(str);
// Return if the str matched the ReGex
return m.Success;
}
// Driver Code.
public static void Main()
{
// Test Case 1:
string str1 = "m" ;
Console.WriteLine(isValidGender(str1));
// Test Case 2:
string str2 = "f" ;
Console.WriteLine(isValidGender(str2));
// Test Case 3:
string str3 = "F" ;
Console.WriteLine(isValidGender(str3));
// Test Case 4:
string str4 = "M" ;
Console.WriteLine(isValidGender(str4));
// Test Case 5:
string str5 = "FEMALE" ;
Console.WriteLine(isValidGender(str5));
// Test Case 6:
string str6 = "S" ;
Console.WriteLine(isValidGender(str6));
// Test case 7:
string str7 = "Not prefer to say" ;
Console.WriteLine(isValidGender(str7));
}
} // This code is contributed by Pushpesh Raj. |
// Javascript program to validate // GENDER using Regular Expression // Function to validate the // GENDER function isValidGender(str) {
// Regex to check valid
// GENDER
let regex = new RegExp(/(?:m|M|male|Male|f|F|female|Female|FEMALE|MALE|Not prefer to say)$/);
// 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 = "m" ;
console.log(isValidGender(str1)); // Test Case 2: let str2 = "f" ;
console.log(isValidGender(str2)); // Test Case 3: let str3 = "F" ;
console.log(isValidGender(str3)); // Test Case 4: let str4 = "M" ;
console.log(isValidGender(str4)); // Test Case 5: let str5 = "FEMALE" ;
console.log(isValidGender(str5)); // Test Case 6: let str6 = "S" ;
console.log(isValidGender(str6)); // Test Case 7: let str7= "Not prefer to say" ;
console.log(isValidGender(str7)); |
true true true true true false true
Time Complexity: O(N) for each testcase, where N is the length of the given string.
Auxiliary Space: O(1)
Related Articles: