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

A Simple IO:

In order to read from a file and write to a file, Java offers FileInputStream and FileOutputStream Classes. FileInputStream's constructor accepts filepath of Input File as argument and creates File Input Stream. Similarly, FileOutputStream's constructor accepts filepath of Output File as argument and creates File Output Stream.After the file handling is done, it's important to "close" the streams.

import java.io.*;
 
public class myIODemo {
    public static void main(String args[]) throws IOException {
        FileInputStream in = null;
        FileOutputStream out = null;
         
        try {
            in = new FileInputStream("//home//user//Documents//InputFile.txt");
            out = new FileOutputStream("//home//user//Documents//OutputFile.txt");
             
            int c;
            while((c = in.read()) != -1) {
                out.write(c);
            }
        } finally {
            if(in != null) {
                in.close();
            }
            if(out != null) {
                out.close();
            }
        }
    }
}

Ref: https://bit.ly/3nwqLNg