Validate Gender using Regular Expressions
Last Updated :
20 Dec, 2022
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: True
Input: 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++
#include <bits/stdc++.h>
#include <regex>
using namespace std;
string isValidGender(string str)
{
const regex pattern(
"(?:m|M|male|Male|f|F|female|Female|FEMALE|MALE|"
"Not prefer to say)$" );
if (str.empty()) {
return "false" ;
}
if (regex_match(str, pattern)) {
return "true" ;
}
else {
return "false" ;
}
}
int main()
{
string str1 = "m" ;
cout << isValidGender(str1) << endl;
string str2 = "f" ;
cout << isValidGender(str2) << endl;
string str3 = "F" ;
cout << isValidGender(str3) << endl;
string str4 = "M" ;
cout << isValidGender(str4) << endl;
string str5 = "MALE" ;
cout << isValidGender(str5) << endl;
string str6 = "S" ;
cout << isValidGender(str6) << endl;
string str7 = "Not prefer to say" ;
cout << isValidGender(str7) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.regex.*;
class GFG {
public static boolean isValidGender(String str)
{
String regex
= "(?:m|M|male|Male|f|F|female|Female|FEMALE|MALE|Not prefer to say)$" ;
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 = "m" ;
System.out.println(isValidGender(str1));
String str2 = "f" ;
System.out.println(isValidGender(str2));
String str3 = "F" ;
System.out.println(isValidGender(str3));
String str4 = "M" ;
System.out.println(isValidGender(str4));
String str5 = "FEMALE" ;
System.out.println(isValidGender(str5));
String str6 = "S" ;
System.out.println(isValidGender(str6));
String str7 = "Not prefer to say" ;
System.out.println(isValidGender(str7));
}
}
|
Python3
import re
def isValidGender( str ):
regex = "(?:m|M|male|Male|f|F|female|Female|FEMALE|MALE|Not prefer to say)$"
p = re. compile (regex)
if ( str = = None ):
return False
if (re.search(p, str )):
return True
else :
return False
if __name__ = = '__main__' :
str1 = "m"
print (isValidGender(str1))
str2 = "f"
print (isValidGender(str2))
str3 = "F"
print (isValidGender(str3))
str4 = "M"
print (isValidGender(str4))
str5 = "FEMALE"
print (isValidGender(str5))
str6 = "S"
print (isValidGender(str6))
str7 = "Not prefer to say"
print (isValidGender(str7))
|
C#
using System;
using System.Text.RegularExpressions;
public class GFG {
public static bool isValidGender( string str)
{
string regex
= "(?:m|M|male|Male|f|F|female|Female|FEMALE|MALE|Not prefer to say)$" ;
Regex p = new Regex(regex);
if (str == null ) {
return false ;
}
Match m = p.Match(str);
return m.Success;
}
public static void Main()
{
string str1 = "m" ;
Console.WriteLine(isValidGender(str1));
string str2 = "f" ;
Console.WriteLine(isValidGender(str2));
string str3 = "F" ;
Console.WriteLine(isValidGender(str3));
string str4 = "M" ;
Console.WriteLine(isValidGender(str4));
string str5 = "FEMALE" ;
Console.WriteLine(isValidGender(str5));
string str6 = "S" ;
Console.WriteLine(isValidGender(str6));
string str7 = "Not prefer to say" ;
Console.WriteLine(isValidGender(str7));
}
}
|
Javascript
function isValidGender(str) {
let regex = new RegExp(/(?:m|M|male|Male|f|F|female|Female|FEMALE|MALE|Not prefer to say)$/);
if (str == null ) {
return "false" ;
}
if (regex.test(str) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "m" ;
console.log(isValidGender(str1));
let str2 = "f" ;
console.log(isValidGender(str2));
let str3 = "F" ;
console.log(isValidGender(str3));
let str4 = "M" ;
console.log(isValidGender(str4));
let str5 = "FEMALE" ;
console.log(isValidGender(str5));
let str6 = "S" ;
console.log(isValidGender(str6));
let str7= "Not prefer to say" ;
console.log(isValidGender(str7));
|
Output
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:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...