w3resource

Java Regular Expression: Exercises, Practice, Solution


This resource offers a total of 150 Java Regular Expression problems for practice. It includes 30 main exercises, each accompanied by solutions, detailed explanations, and four related problems.

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

1. Check Alphanumeric String

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. Match 'p' Followed by 'q's

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. Match Lowercase with Underscore

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

4. Match Uppercase Followed by Lowercase

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. Match 'p...q' Pattern

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

6. Check if Word Contains 'g'

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

7. Match Word with Middle 'g'

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. Match Alphanumeric Underscore

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

9. String Starts with Specific Number

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

10. Remove Leading Zeros from IP

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

11. Check Number at End of String

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

12. Replace Python and Code Words

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

13. Python Detection in String

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. Count Vowels with Regex

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. Remove Vowels from String

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

16. Replace Vowels with Character

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

17. Count Decimal Places

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

18. Validate PIN Code

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. Remove 'p', 'q', 'r' Letters

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. Add Thousand Separator

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

21. Remove Non-Alphanumeric

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

22. Validate Phone Number

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

23. Move Lowercase to Front

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. Separate Consonants and Vowels

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

25. Get Last N Vowels

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

26. Validate Hex Code

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

27. Dash Around Vowels

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

28. Reverse Long Words

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

29. Check Mathematical Expression

Write a Java program to check if a given string is a mathematical expression or not.
Click me to see the solution

30. Dash Between Upper and Lower Case

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.