- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Name validation using Java Regular Expressions
The name can be validated using the java.util.regex.Pattern.matches() method. This method matches the regular expression for the name and the given input name and returns true if they match and false otherwise.
A program that demonstrates this is given as follows:
Example
public class Demo { public static void main(String args[]) { String name = "John Harry Smith"; String regexName = "\p{Upper}(\p{Lower}+\s?)"; String patternName = "(" + regexName + "){2,3}"; System.out.println("The name is: " + name); System.out.println("Is the above name valid? " + name.matches(patternName)); } }
Output
The name is: John Harry Smith Is the above name valid? true
Now let us understand the above program.
The name is printed. The Pattern.matches() method matches the regular expression for the name and the given input name and the result is printed. A code snippet which demonstrates this is as follows:
String name = "John Harry Smith"; String regexName = "\p{Upper}(\p{Lower}+\s?)"; String patternName = "(" + regexName + "){2,3}"; System.out.println("The name is: " + name); System.out.println("Is the above name valid? " + name.matches(patternName));
- Related Articles
- Date validation using Java Regular Expressions
- Phone Number validation using Java Regular Expressions
- Zip Code validation using Java Regular Expressions
- Validate the first name and last name with Java Regular Expressions
- Java Regular Expressions Tutorial
- Java regular expressions sample examples
- Possessive quantifiers Java Regular expressions
- Java Regular expressions Logical operators
- Reluctant quantifiers Java Regular expressions
- Program to check valid mobile number using Java regular expressions
- Checking for valid email address using regular expressions in Java
- How to validate an email address using Java regular expressions.
- Greedy quantifiers Java Regular expressions in java.
- Validate Phone with Java Regular Expressions
- Back references in Java regular expressions

Advertisements