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.
Java: Tips of the Day
How to convert Java String into byte[]?
The object your method decompressGZIP() needs is a byte[].
So the basic, technical answer to the question you have asked is:
byte[] b = string.getBytes(); byte[] b = string.getBytes(Charset.forName("UTF-8")); byte[] b = string.getBytes(StandardCharsets.UTF_8); // Java 7+ only
However the problem you appear to be wrestling with is that this doesn't display very well. Calling toString() will just give you the default Object.toString() which is the class name + memory address. In your result [[email protected], the [B means byte[] and 38ee9f13 is the memory address, separated by an @.
For display purposes you can use:
Arrays.toString(bytes);
But this will just display as a sequence of comma-separated integers, which may or may not be what you want.
To get a readable String back from a byte[], use:
String string = new String(byte[] bytes, Charset charset);
The reason the Charset version is favoured, is that all String objects in Java are stored internally as UTF-16. When converting to a byte[] you will get a different breakdown of bytes for the given glyphs of that String, depending upon the chosen charset.
Ref: https://bit.ly/3ludT8R
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework