Remove uppercase, lowercase, special, numeric, and non-numeric characters from a String

03 Nov, 2021
Given string str of length N, the task is to remove uppercase, lowercase, special, numeric, and non-numeric characters from this string and print the string after the simultaneous modifications.
Examples:
Input: str = “GFGgfg123$%”
Output: After removing uppercase characters: gfg123$%
             After removing lowercase characters: GFG123$%
            After removing special characters: GFGgfg123
           After removing numeric characters: GFGgfg$%
           After removing non-numeric characters: 123
Input: str = “J@va12”
Output: After removing uppercase characters: @va12
             After removing lowercase characters: J@12
            After removing special characters: Jva12
           After removing numeric characters: J@va           
          After removing non-numeric characters: 12
Naive Approach: The simplest approach is to iterate over the string and remove uppercase, lowercase, special, numeric, and non-numeric characters. Below are the steps:
1. Traverse the string character by character from start to end.
2. Check the ASCII value of each character for the following conditions:
If the ASCII value lies in the range of [65, 90], then it is an uppercase character. Therefore, skip such characters and add the rest characters in another string and print it.
If the ASCII value lies in the range of [97, 122], then it is a lowercase character. Therefore, skip such characters and add the rest characters in another string and print it.
If the ASCII value lies in the range of [32, 47], [58, 64], [91, 96], or [123, 126] then it is a special character. Therefore, skip such characters and add the rest characters in another string and print it.
If the ASCII value lies in the range of [48, 57], then it is a numeric character. Therefore, skip such characters and add the rest characters in another string and print it.
Else the character is a non-numeric character. Therefore, skip such characters and add the rest characters in another string and print it.
Time Complexity: O(N)Auxiliary Space: O(1)
 Regular Expression Approach: The idea is to use regular expressions to solve this problem. Below are the steps:
1. Create regular expressions to remove uppercase, lowercase, special, numeric, and non-numeric characters from the string as mentioned below:
regexToRemoveUpperCaseCharacters  = “[A-Z]”
regexToRemoveLowerCaseCharacters = “[a-z]”
regexToRemoveSpecialCharacters = “[^A-Za-z0-9]”
regexToRemoveNumericCharacters = “[0-9]”
regexToRemoveNon-NumericCharacters = “[^0-9]”
2. Compile the given regular expressions to create the pattern using Pattern.compile() method.
3. Match the given string with all the above Regular Expressions using Pattern.matcher().
4. Replace every matched pattern with the target string using the Matcher.replaceAll() method.
Below is the implementation of the above approach:
C++
Java
Python3
// C++ program to remove uppercase, lowercase// special, numeric, and non-numeric characters#include <iostream>#include <regex>using namespace std; // Function to remove uppercase charactersstring removingUpperCaseCharacters(string str){  // Create a regular expression  const regex pattern("[A-Z]");   // Replace every matched pattern with the  // target string using regex_replace() method  return regex_replace(str, pattern, "");} // Function to remove lowercase charactersstring removingLowerCaseCharacters(string str){  // Create a regular expression  const regex pattern("[a-z]");   // Replace every matched pattern with the  // target string using regex_replace() method  return regex_replace(str, pattern, "");} // Function to remove special charactersstring removingSpecialCharacters(string str){  // Create a regular expression  const regex pattern("[^A-Za-z0-9]");   // Replace every matched pattern with the  // target string using regex_replace() method  return regex_replace(str, pattern, "");} // Function to remove numeric charactersstring removingNumericCharacters(string str){  // Create a regular expression  const regex pattern("[0-9]");   // Replace every matched pattern with the  // target string using regex_replace() method  return regex_replace(str, pattern, "");} // Function to remove non-numeric charactersstring removingNonNumericCharacters(string str){  // Create a regular expression  const regex pattern("[^0-9]");   // Replace every matched pattern with the  // target string using regex_replace() method  return regex_replace(str, pattern, "");} int main(){  // Given String str  string str = "GFGgfg123$%";   // Print the strings after the simultaneous  // modifications  cout << "After removing uppercase characters: "       << removingUpperCaseCharacters(str) << endl;  cout << "After removing lowercase characters: "       << removingLowerCaseCharacters(str) << endl;  cout << "After removing special characters: "       << removingSpecialCharacters(str) << endl;  cout << "After removing numeric characters: "       << removingNumericCharacters(str) << endl;  cout << "After removing non-numeric characters: "       << removingNonNumericCharacters(str) << endl;   return 0;} // This article is contributed by yuvraj_chandra
// Java program to remove uppercase, lowercase// special, numeric, and non-numeric charactersimport java.util.regex.Matcher;import java.util.regex.Pattern;public class GFG{     // Function to remove uppercase characters    public static String    removingUpperCaseCharacters(String str)    {         // Create a regular expression        String regex = "[A-Z]";         // Compile the regex to create pattern        // using compile() method        Pattern pattern = Pattern.compile(regex);         // Get a matcher object from pattern        Matcher matcher = pattern.matcher(str);         // Replace every matched pattern with the        // target string using replaceAll() method        return matcher.replaceAll("");    }     // Function to remove lowercase characters    public static String    removingLowerCaseCharacters(String str)    {         // Create a regular expression        String regex = "[a-z]";         // Compile the regex to create pattern        // using compile() method        Pattern pattern = Pattern.compile(regex);         // Get a matcher object from pattern        Matcher matcher = pattern.matcher(str);         // Replace every matched pattern with the        // target string using replaceAll() method        return matcher.replaceAll("");    }     // Function to remove special characters    public static String    removingSpecialCharacters(String str)    {         // Create a regular expression        String regex = "[^A-Za-z0-9]";         // Compile the regex to create pattern        // using compile() method        Pattern pattern = Pattern.compile(regex);         // Get a matcher object from pattern        Matcher matcher = pattern.matcher(str);         // Replace every matched pattern with the        // target string using replaceAll() method        return matcher.replaceAll("");    }     // Function to remove numeric characters    public static String    removingNumericCharacters(String str)    {         // Create a regular expression        String regex = "[0-9]";         // Compile the regex to create pattern        // using compile() method        Pattern pattern = Pattern.compile(regex);         // Get a matcher object from pattern        Matcher matcher = pattern.matcher(str);         // Replace every matched pattern with the        // target string using replaceAll() method        return matcher.replaceAll("");    }     // Function to remove non-numeric characters    public static String    removingNonNumericCharacters(String str)    {         // Create a regular expression        String regex = "[^0-9]";         // Compile the regex to create pattern        // using compile() method        Pattern pattern = Pattern.compile(regex);         // Get a matcher object from pattern        Matcher matcher = pattern.matcher(str);         // Replace every matched pattern with the        // target string using replaceAll() method        return matcher.replaceAll("");    }     // Driver Code    public static void main(String[] args)    {        // Given String str        String str = "GFGgfg123$%";         // Print the strings after the simultaneous        // modifications        System.out.println(            "After removing uppercase characters: "            + removingUpperCaseCharacters(str));        System.out.println(            "After removing lowercase characters: "            + removingLowerCaseCharacters(str));        System.out.println(            "After removing special characters: "            + removingSpecialCharacters(str));        System.out.println(            "After removing numeric characters: "            + removingNumericCharacters(str));        System.out.println(            "After removing non-numeric characters: "            + removingNonNumericCharacters(str));    }}
# Python3 program to remove# uppercase, lowercase special,# numeric, and non-numeric charactersimport re # Function to remove# uppercase charactersdef removingUpperCaseCharacters(str):     # Create a regular expression    regex = "[A-Z]"     # Replace every matched pattern     # with the target string using    # sub() method    return (re.sub(regex, "", str)) # Function to remove lowercase# charactersdef removingLowerCaseCharacters(str):     # Create a regular expression    regex = "[a-z]"     # Replace every matched    # pattern with the target    # string using sub() method    return (re.sub(regex, "", str)) def removingSpecialCharacters(str):     # Create a regular expression    regex = "[^A-Za-z0-9]"     # Replace every matched pattern    # with the target string using    # sub() method    return (re.sub(regex, "", str)) def removingNumericCharacters(str):     # Create a regular expression    regex = "[0-9]"     # Replace every matched    # pattern with the target    # string using sub() method    return (re.sub(regex, "", str)) def  removingNonNumericCharacters(str):     # Create a regular expression    regex = "[^0-9]"     # Replace every matched pattern    # with the target string using    # sub() method    return (re.sub(regex, "", str)) str = "GFGgfg123$%"print("After removing uppercase characters:",       removingUpperCaseCharacters(str))print("After removing lowercase characters:",       removingLowerCaseCharacters(str))print("After removing special characters:",       removingSpecialCharacters(str))print("After removing numeric characters:",       removingNumericCharacters(str))print("After removing non-numeric characters:",       removingNonNumericCharacters(str)) # This code is contributed by avanitrachhadiya2155
After removing uppercase characters: gfg123$%
After removing lowercase characters: GFG123$%
After removing special characters: GFGgfg123
After removing numeric characters: GFGgfg$%
After removing non-numeric characters: 123
Time Complexity: O(N)
Auxiliary Space: O(1)
avanitrachhadiya2155
yuvraj_chandra
rohitsingh07052
Java-String-Programs
regular-expression
Java
Java Programs
Strings
Strings
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.