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

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:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to remove uppercase, lowercase
// special, numeric, and non-numeric characters
import 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));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to remove
# uppercase, lowercase special,
# numeric, and non-numeric characters
import re
 
# Function to remove
# uppercase characters
def 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
# characters
def 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

chevron_right


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



Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

External Technical Content Reviewer at GeeksforGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : avanitrachhadiya2155