How to validate identifier using Regular Expression in Java Last Updated : 31 Jan, 2023 Comments Improve Suggest changes Like Article Like Report 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: False 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_$]*)$"; 3. 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 underscore(_) or dollar sign($) after the first character of the string. It contains one or more time.$ represents the ending of the string. 4. Match the given string with the Regex. In Java, this can be done using Pattern.matcher(). 5. Return true if the string matches with the given regex, else return false. Below is the implementation of the above approach: Java // 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)); } } Output: true false false Time complexity : O(1) Space complexity : O(1) Comment More infoAdvertise with us Next Article How to validate identifier using Regular Expression in Java P prashant_srivastava Follow Improve Article Tags : Strings Java Java Programs DSA java-regular-expression +1 More Practice Tags : JavaStrings Similar Reads How to validate a Password using Regular Expressions in Java Given a password, the task is to validate the password with the help of Regular Expression. A password is considered valid if all the following constraints are satisfied: It contains at least 8 characters and at most 20 characters.It contains at least one digit.It contains at least one upper case al 3 min read Finding Data Type of User Input using Regular Expression in Java Given a string, the task is to find its corresponding datatype using regular expression in java. We can broadly classify all data types into following types: Integer: Numeric datatypes like byte, short, int, long take the form of an Integer object.Double: Decimal datatypes like float and double take 2 min read How to Validate if a String Starts with a Vowel Using Regex in Java? Regular Expressions in Java allow developers to create patterns for matching strings. In this article, we will learn How to Check whether the Given Character String Starts with a Vowel. Example to Check String is Starting with a VowelInput: "Apple"Output: Is a VowelInput: "Cart"Output: Is not a Vowe 2 min read Validate a Time Format (HH:mm:ss) Using Regex in Java In this article, we will explore how to use regex to validate the used time format HH:mm:ss. Regular expressions (regex) offer a method, for validating time formats ensuring that they follow patterns which is mentioned below in the article. How to Validate a Time Format (HH:mm:ss) Using Regex?When t 2 min read Check if a String Contains Only Alphabets in Java using ASCII Values Given a string, now we all know that the task is to check whether a string contains only alphabets. Now we will be iterating character by character and checking the corresponding ASCII value attached to it. If not found means there is some other character other than "a-z" or "A-Z". If we traverse th 4 min read Like