Understanding Regular Expressions in Java: Password Validation Example
This article introduces regular expressions through a Java password‑validation case, compares a manual character‑checking implementation with a concise regex solution, explains regex syntax and meta‑characters, and demonstrates how to apply Pattern.matches for string validation in Java.
Before learning regular expressions, the article presents a realistic scenario where a website requires passwords to be 6‑16 characters long and contain digits, uppercase letters, lowercase letters, and specific special symbols. It shows how a programmer might initially implement this validation with explicit character checks.
The first implementation is a Java method public static boolean checkPassword(String password) { ... } that verifies length, iterates over each character, sets flags for numbers, lowercase, uppercase, and special characters, and returns true only if all conditions are met. The method is tested with eight sample passwords, confirming its correctness but highlighting its verbosity (about 30 lines of code).
To simplify the logic, the article introduces regular expressions as a powerful alternative. It provides a compact one‑line method public static boolean checkPasswordByRegex(String password) { return Pattern.matches("^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[~@#S%*_\\-+=:.?])[A-Za-z0-9~@#S%*_\\-+=:.?]{8,20}$", password); } , which achieves the same validation with far less code. The same test passwords are used to demonstrate that the regex‑based method also works as expected.
The article then explains what a regular expression (regex) is, its naming conventions (e.g., regexp or regexps ), and its structure, distinguishing between ordinary characters and meta‑characters. It lists common meta‑characters such as \d , \D , \w , \W , \s , \S , . , | , character classes like [abc] , negated classes [^abc] , ranges [a‑z] , quantifiers ? , + , * , {n} , {n,m} , and back‑references \1 , \2 . Each meta‑character is illustrated with examples and accompanying screenshots.
Finally, the article outlines typical use cases for regex in Java: validating string patterns (e.g., passwords), performing replacements, and extracting specific characters. It shows the basic Java API call boolean result = Pattern.matches(regex, input); and explains the meaning of the regex pattern and input string. The article concludes by encouraging readers to learn regex despite its learning curve, as it greatly simplifies string processing tasks.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.