How to validate identifier using Regular Expression in Java
Given a string str, the task is to check whether the string is a valid identifier or not using the Regular Expression.
The valid rules for defining Java identifiers are:
- It must start with either lower case alphabet[a-z] or upper case alphabet[A-Z] or underscore(_) or a dollar sign($).
- It should be a single word, the white spaces are not allowed.
- It should not start with digits.
Examples:
Input: str = “$geeks123”
Output: True
Explanation: The given string satisfies all the above mentioned conditions.Input: str = “$gee ks123”
Output: False
Explanation: The given string contains white spaces, therefore it is not a valid identifier.Input: str = “1geeks$”
Output: True
Explanation: The given string start with digit, therefore it is not a valid identifier.
Approach: This problem can be solved by using Regular Expression.
- Get the string.
- Create a regex to check the valid identifiers.
regex = "^([a-zA-Z_$][a-zA-Z\\d_$]*)$";
where:
- ^ represents the starting character of the string.
- [a-zA-Z_$] represents, the string start with only lower case alphabet or upper case alphabet or underscore(_) or dollar sign($).>/li>
- [a-zA-Z\\d_$]* represents, the string can be alphanumeric or uderscore(_) or dollar sign($) after the first character of the string. It contains one or more time.
- $ represents the ending of the string.
- Match the given string with the Regex. In Java, this can be done using Pattern.matcher().
- Return true if the string matches with the given regex, else return false.
Below is the implementation of the above approach:
// Java program to validate the // identifiers using Regular Expression. import java.util.regex.*; class GFG { // Function to validate the identifier. public static boolean isValidIdentifier(String identifier) { // Regex to check valid identifier. String regex = "^([a-zA-Z_$][a-zA-Z\\d_$]*)$"; // Compile the ReGex Pattern p = Pattern.compile(regex); // If the identifier is empty // return false if (identifier == null) { return false; } // Pattern class contains matcher() method // to find matching between given identifier // and regular expression. Matcher m = p.matcher(identifier); // Return if the identifier // matched the ReGex return m.matches(); } // Driver Code. public static void main(String args[]) { // Test Case 1: String str1 = "$geeks123"; System.out.println(isValidIdentifier(str1)); // Test Case 2: String str2 = "$gee ks123"; System.out.println(isValidIdentifier(str2)); // Test Case 3: String str3 = "1geeks$"; System.out.println(isValidIdentifier(str3)); } } |
true false false
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
Recommended Posts:
- How to validate a domain name using Regular Expression
- How to validate HTML tag using Regular Expression
- How to validate PAN Card number using Regular Expression
- How to validate pin code of India using Regular Expression
- How to validate SSN (Social Security Number) using Regular Expression
- How to validate time in 12-hour format using Regular Expression
- How to validate time in 24-hour format using Regular Expression
- How to validate image file extension using Regular Expression
- How to validate Hexadecimal Color Code using Regular Expression
- How to validate a Password using Regular Expressions in Java
- How to validate an IP address using Regular Expressions in Java
- How to validate a Username using Regular Expressions in Java
- Check if a given string is a valid number (Integer or Floating Point) in Java | SET 2 (Regular Expression approach)
- Validating Roman Numerals Using Regular expression
- std::regex_match, std::regex_replace() | Regex (Regular Expression) In C++
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

