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 prashant_srivastava Follow Improve Article Tags : Strings Java Java Programs DSA java-regular-expression +1 More Practice Tags : JavaStrings Similar Reads DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on 7 min read Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s 10 min read Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per 15+ min read Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it, 13 min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st 2 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta 15+ min read Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc 15 min read Like