Given some phrases of Gender, the duty is to verify if they’re legitimate or not utilizing common expressions. The right responses might be as given under:
- Male / male / MALE / M / m
- Feminine / feminine / FEMALE / F / f
- Not desire to say
Instance:
Enter: M
Output: True
Enter: S
Output: False
Method: The issue might be solved primarily based on the next concept:
Create a regex sample to validate the quantity as written under:
regex= “(?:m|M|male|Male|f|F|feminine|Feminine|FEMALE|MALE|Not desire to say)$”
Simply verify whether or not any of the phrases are current in regex or not utilizing (?:)
Observe the under steps to implement the thought:
- Create a daily expression for Gender.
- If the Enter string is empty, return False.
- Else, Use the Sample class to compile the regex fashioned.
- Use the matcher perform to verify whether or not the Gender Is legitimate or not.
- Whether it is legitimate, return true. In any other case, return false.
Beneath is the Implementation of the above method:
C++
#embrace <bits/stdc++.h>
#embrace <regex>
utilizing namespace std;
string isValidGender(string str)
{
const regex sample(
"(?:m|M|male|Male|f|F|feminine|Feminine|FEMALE|MALE|"
"Not desire to say)$" );
if (str.empty()) {
return "false" ;
}
if (regex_match(str, sample)) {
return "true" ;
}
else {
return "false" ;
}
}
int foremost()
{
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 desire 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|feminine|Feminine|FEMALE|MALE|Not desire to say)$" ;
Sample p = Sample.compile(regex);
if (str == null ) {
return false ;
}
Matcher m = p.matcher(str);
return m.matches();
}
public static void foremost(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 desire to say" ;
System.out.println(isValidGender(str7));
}
}
|
Python3
import re
def isValidGender( str ):
regex = "(?:m|M|male|Male|f|F|feminine|Feminine|FEMALE|MALE|Not desire 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 desire to say"
print (isValidGender(str7))
|
Javascript
perform isValidGender(str) {
let regex = new RegExp(/(?:m|M|male|Male|f|F|feminine|Feminine|FEMALE|MALE|Not desire to say)$/);
if (str == null ) {
return "false" ;
}
if (regex.take a look at(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 desire to say" ;
console.log(isValidGender(str7));
|
Output
true
true
true
true
true
false
true
Associated Articles: