News Leaflets
A leading news portal.

Validate Gender using Regular Expressions

0

Improve Article

Save Article

Like Article

Improve Article

Save Article

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

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

Related Articles:

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! News Leaflets is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment
vulvatube.com teenpornvideo.mobi desi girls sexy
sex video in bus tubekitty.mobi kamapichachi
the broken marriage vow march 12 full episode teleseryepisode.com mateo lorenzo net worth
telugu hd sex pornflex.org bluefilmtamil
indian sex stories lesbian indaporn.com best sex mms
hinde xnxx video redwap2.com bangla chudachudi
طيز خليجي filmstreamingporno.com التحرش بالمنقبات
affair sex video ultraporn.mobi deshimagi
tubb99 nuporn.mobi mumbaixvideo
agimat ng agila cast watchpinoyteleserye.com stl today result
age of tamanna pimpmovs.com xxxxx inden
indan xvideo com xxx-tube-list.net indina six video
gonzo xxx sunny leone eporner.name desi pirn
بنت تلعب في كسها teentubeonline.com سكس امهات اسيوي
largeporn film tubepatrol.porn kama katai