w3resource

Java Regular Expression: Exercises, Practice, Solution

Java Regular Expression [30 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts.  Go to the editor]

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).
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.
Click me to see the solution

3. Write a Java program to find sequences of lowercase letters joined by an underscore.
Click me to see the solution

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

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

6. Write a Java program to check if a word contains the character 'g' in a given string.
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.
Click me to see the solution

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

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

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

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

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

13. Write a Java program to find the word Python in a given string. If the word Python appears in the string return Java otherwise return C++. Ignore case sensitive.
Click me to see the solution

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

15. Write a Java program to remove all vowels from a given string. Return the updated string.
Click me to see the solution

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

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

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

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

20. Write a Java program that takes a number and sets a thousand separators for that number.
Click me to see the solution

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

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

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

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

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

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

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

28. Write a Java program to reverse words longer than 3 in a given string.
Click me to see the solution

29. Write a Java program to check if a given string is a mathematical expression or not.
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 string.
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.



Follow us on Facebook and Twitter for latest update.