w3resource

Java Regular Expression: Exercises, Practice, Solution

Java Regular Expression [30 exercises with solution]

1. Write a Java program to check whether a string contains only a certain set of characters (in this case a-z, A-Z and 0-9). Go to the editor
Click me to see the solution

2. Write a Java program that matches a string that has a p followed by zero or more q's. Go to the editor
Click me to see the solution

3. Write a Java program to find sequences of lowercase letters joined with a underscore. Go to the editor
Click me to see the solution

4. Write a Java program to find the sequences of one upper case letter followed by lower case letters. Go to the editor
Click me to see the solution

5. Write a Java program that matches a string that has a 'p' followed by anything, ending in 'q'. Go to the editor
Click me to see the solution

6. Write a Java program to check a word contains the character 'g' in a given string. Go to the editor
Click me to see the solution

7. Write a Java program that matches a word containing 'g', not at the start or end of the word. Go to the editor
Click me to see the solution

8. Write a Java program to match a string that contains only upper and lowercase letters, numbers, and underscores. Go to the editor
Click me to see the solution

9. Write a Java program where a string starts with a specific number. Go to the editor
Click me to see the solution

10. Write a Java program to remove leading zeros from a given IP address. Go to the editor
Click me to see the solution

11. Write a Java program to check for a number at the end of a given string. Go to the editor
Click me to see the solution

12. Write a Java program to replace Python with Java and code with coding in a given string. Go to the editor
Click me to see the solution

13. Write a Java program to find the word Python in a given string, if the word Python present in the string return Java otherwise return C++. Ignore case sensitive. Go to the editor
Click me to see the solution

14. Write a Java program to count number of vowels in a given string using regular expression. Go to the editor
Click me to see the solution

15. Write a Java program to remove all the vowels of a given string. Return the new string. Go to the editor
Click me to see the solution

16. Write a Java program to replace all the vowels in a given string with a specified character. Go to the editor
Click me to see the solution

17. Write a Java program to count the number of decimal places in a given number. Go to the editor
Click me to see the solution

18. Write a Java program to validate a personal identification number (PIN). Assume the length of a PIN number is 4, 6 or 8. Go to the editor
Click me to see the solution

19. Write a Java program to remove the specific letters from a string and return the new string. Specific letters: "p", "q", or "r". Go to the editor
Click me to see the solution

20. Write a Java program that takes a number and set thousands separator in that number. Go to the editor
Click me to see the solution

21. Write a Java program to remove all non-alphanumeric characters from a given string. Go to the editor
Click me to see the solution

22. Write a Java program to validate a given phone number. Go to the editor
Click me to see the solution

23. Write a Java program to move all lower case letters to the front of a given word keeping the relative position all the letters(both upper and lower case) same. Go to the editor
Click me to see the solution

24. Write a Java program to separate consonants and vowels from a given string. Go to the editor
Click me to see the solution

25. Write a Java program to get last n vowels of a given string. Go to the editor
Click me to see the solution

26. Write a Java program to check whether a given string is a valid hex code or not. Go to the editor
Click me to see the solution

27. Write a Java program to add a dash before and after every vowel in a given string. Go to the editor
Click me to see the solution

28. Write a Java program to reverse the words of length higher than 3 in a given string. Go to the editor
Click me to see the solution

29. Write a Java program to check if a given string is a Mathematical Expression or not. Go to the editor
Click me to see the solution

30. Write a Java program to insert a dash (-) between an upper case letter and a lower case letter in a given string. Go to the editor
Click me to see the solution

 

Java Code Editor

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Share this Tutorial / Exercise on : Facebook and Twitter

Java: Tips of the Day

Java: mask

Replaces all but the last num of characters with the specified mask character:

public static String mask(String input, int num, String mask) {
    int length = input.length();
    return num > 0
            ?
            input.substring(0, length - num).replaceAll(".", mask)
                    + input.substring(length - num)
            :
            input.substring(0, Math.negateExact(num))
                    + input.substring(Math.negateExact(num), length).replaceAll(".", mask);
}

Ref: https://bit.ly/37eOVEG