
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Validate the first name and last name with Java Regular Expressions
In order to match the first name and last name using regular expression, we use the matches method in Java. The java.lang.String.matches() method returns a boolean value which depends on the matching of the String with the regular expression.
Declaration −The java.lang.String.matches() method is declared as follows −
public boolean matches(String regex)
Let us see a program to validate the first name and last name with regular expressions −
Example
public class Example { public static void main( String[] args ) { System.out.println(firstName("Tom")); System.out.println(lastName("hanks")); } // validate first name public static boolean firstName( String firstName ) { return firstName.matches( "[A-Z][a-z]*" ); } // validate last name public static boolean lastName( String lastName ) { return lastName.matches( "[A-Z]+([ '-][a-zA-Z]+)*" ); } }
Output
true false
- Related Questions & Answers
- Name validation using Java Regular Expressions
- Validate Phone with Java Regular Expressions
- Split First name and Last name using JavaScript?
- Validate city and state with Java Regular Expressions
- Validate the ZIP code with Java Regular expressions
- Constructing full name from first name, last name and optional middle name in JavaScript
- MySQL query to display columns name first name, last name as full name in a single column?
- Java program to print the initials of a name with last name in full
- Display records where first and last name begins with the same letter in MySQL
- MySQL GROUP BY and CONCAT() to display distinct first and last name
- Search and Replace with Java regular expressions
- How to validate an email address using Java regular expressions.
- How to recognize full name and last name in Open NLP?
- How to create separate columns for first and last name in R?
- Python program to print the initials of a name with last name in full?
Advertisements