Regular Expressions

Regular Expressions

hi tried writing this program and got stuck give some help please


Write a java program to accept name, phone number , address,email and validate them.
Name should Start with upper case and the lower cases, phone number Should start with +256-041-369024.


Thanx for yo help.
View Answers

July 22, 2010 at 4:03 PM

Hi Friend,

Try the following code:

import java.util.*;
import java.util.regex.*;
class RegularExpressions{

public static boolean validateName( String name ) {
return name.matches( "\\p{Upper}(\\p{Lower}+\\s?)" );
}
public static boolean validateAddress( String address )
{
return address.matches( "\\d+\\s+([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)" );

}
public static boolean validatePhone( String phone )
{
return phone.matches( "^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{6})$" );
}
public static boolean isEmailValid(String email){
boolean isValid = false;
String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
CharSequence inputStr = email;
Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
if(matcher.matches()){
isValid = true;
}
return isValid;
}


public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.print("Enter Name: ");
String name=input.nextLine();
while(validateName(name)!=true){
System.out.print("Invalid Name!");
System.out.println();
System.out.print("Enter Name: ");
name=input.nextLine();
}

System.out.print("Enter Phone Number: ");
String phone=input.nextLine();
while(validatePhone(phone)!=true){
System.out.print("Invalid PhoneNumber!");
System.out.println();
System.out.print("Enter Phone Number: ");
phone=input.nextLine();
}

System.out.print("Enter Address: ");
String address=input.nextLine();
while(validateAddress(address)!=true){
System.out.print("Invalid Address!");
System.out.println();
System.out.print("Enter Address: ");
address=input.nextLine();
}

System.out.print("Enter EmailId: ");
String email=input.nextLine();
while(isEmailValid(email)!=true){
System.out.print("Invalid Email!");
System.out.println();
System.out.print("Enter EmailID: ");
email=input.nextLine();
}

}
}

Thanks









Related Tutorials/Questions & Answers:
Regular expressions
Regular expressions  Sir/Madam, please tell me the meaning of the following code import java.io.*; import java.util.*; class Demo{ public static void main(String args[]){ double d=1000000.1232323d
Regarding Regular Expressions
Regarding Regular Expressions  Hi I would like to supply my regular..., it will return the default value as i have mentioned in the Regular Expressions. Please acknowledge me as soon as possible. Thanks, K.ManojKumar.
Advertisements
Regular Expressions - Java Beginners
Regular Expressions  hi tried writing this program and got stuck give some help please Write a java program to accept name, phone number , address,email and validate them. Name should Start with upper case and the lower
Java Validations using Regular Expressions
Java Validations using Regular Expressions In this section, you will learn how to validate different fields using regular expressions. Here we have created the methods to validate the name,address, email and phone number entered
How to use regular expressions - Java Beginners
How to use regular expressions  how do i put a label and text field on a frame  Hi Friend, Here i am sending you full source code in Java Swing to put label and text field on a frame. // LabelTextSwing.java
Matching Address using regular expressions
Matching Address using regular expressions  ... using regular expression.For this we are going to make program named...}":-Causes the resulting regular expression to match from 2 to 3 repetitions
i want validation for one text field and text area,email,combobox, in that email will validte by useing regular expressions?
i want validation for one text field and text area,email,combobox, in that email will validte by useing regular expressions?  i want validation... regular expressions?   <html> <h2>Form Validation<
Regular Expressions
Find.java.    Matching Address using regular expressions... Regular Expressions      ... Regularexpression.    Print Matchingwords using Regular expression
Regular expression
Regular expression  how i write a regular expression for a pattern which present in between
Regularexpressions Tutorial
Regular Expressions Introduction In this tutorial I will show you how to use Regular Expressions in Java. What is Regular Expressions?ADS_TO_REPLACE_1... for. Here are Regular Expressions examples: Split string after each character
Regular expression
Regular expression  can some one explain how below regex works, ndmurl1=${ndmurl#${ndmurl
Regular expression
Regular expression  I have the regular expression : var x=/(^\d+$)/ which allows only numbers. It does not accept character, decimals. However it accepts zero. I want an expression that takes numbers(no decimals)except zero
Identify correct and incorrect conditional expressions, BETWEEN expressions, IN expressions, LIKE expressions, and comparison expressions.
Identify correct and incorrect conditional expressions, BETWEEN expressions, IN expressions, LIKE expressions, and comparison expressions. Prev Chapter 9. EJB-QL Next   
ModuleNotFoundError: No module named 'expressions'
ModuleNotFoundError: No module named 'expressions'  Hi, My Python... 'expressions' How to remove the ModuleNotFoundError: No module named 'expressions' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'expressions'
ModuleNotFoundError: No module named 'expressions'  Hi, My Python... 'expressions' How to remove the ModuleNotFoundError: No module named 'expressions' error? Thanks   Hi, In your python environment
Regular expression
Regular expression  how i extract string Coalition of civil society organizations and CSOs from string:- CSOs : What happening in Yemen is political crisis SANA'A, May 16 (Saba)- Coalition of civil society organizations (CSOs
Form Validation using Regular Expressions is JavaScript
Form Validation using Regular Expressions is JavaScript... is JavaScript validation using Regular Expressions? The JavaScript Validating... incorporated regular expressions. This article I will present a brief tutorial
Applying Regular Expressions on the Contents of a File
Applying Regular Expressions on the Contents of a File       This section illustrates you how to search a string in a file content. Following takes the file name from
PHP Regular Expression
_TO_REPLACE_1 POSIX Regular Expressions PERL Style Regular Expressions PHP Examples...PHP regular expression allows programmer to perform various task like comparing, finding and replacing a particular type of strings. Using Regular
javascript regular expression
javascript regular expression  i need a regular expression code for JavaScript
A regular expression contains another regular expression - PHP
A regular expression contains another regular expression   Please explain how it works in PHP... RegEX1 = "a.*b"; RegEx2 = "a1.*b"; Thanks
split string with regular expression
split string with regular expression  How to split string with regular expression in Java
regular expression for special characters
regular expression for special characters  How to find out regular expression for special characters
ModuleNotFoundError: No module named 'regular'
ModuleNotFoundError: No module named 'regular'  Hi, My Python... 'regular' How to remove the ModuleNotFoundError: No module named 'regular... to install padas library. You can install regular python with following command
using regular expression
using regular expression  How can we extract string 'abc.com ' from a string http://[email protected] using regular expression of php
java tree expressions - XML
java tree expressions  hi all, i have a problem in tree expressions. see there is a document tree like a parent document has no. of child documents. the problem is whenever i click on + sign it means it will have surely have
ModuleNotFoundError: No module named 'rapidpro-expressions'
ModuleNotFoundError: No module named 'rapidpro-expressions'  Hi...: No module named 'rapidpro-expressions' How to remove the ModuleNotFoundError: No module named 'rapidpro-expressions' error? Thanks   Hi
ModuleNotFoundError: No module named 'tag-expressions'
ModuleNotFoundError: No module named 'tag-expressions'  Hi, My... named 'tag-expressions' How to remove the ModuleNotFoundError: No module named 'tag-expressions' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'xpath-expressions'
ModuleNotFoundError: No module named 'xpath-expressions'  Hi, My... named 'xpath-expressions' How to remove the ModuleNotFoundError: No module named 'xpath-expressions' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'ccl-expressions'
ModuleNotFoundError: No module named 'ccl-expressions'  Hi, My... named 'ccl-expressions' How to remove the ModuleNotFoundError: No module named 'ccl-expressions' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'deep-expressions'
ModuleNotFoundError: No module named 'deep-expressions'  Hi, My... named 'deep-expressions' How to remove the ModuleNotFoundError: No module named 'deep-expressions' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'easy-expressions'
ModuleNotFoundError: No module named 'easy-expressions'  Hi, My... named 'easy-expressions' How to remove the ModuleNotFoundError: No module named 'easy-expressions' error? Thanks   Hi, In your
Version of com.laamella>s-expressions dependency
List of Version of com.laamella>s-expressions dependency
regular expression for phone number
regular expression for phone number  Hi, i need the regular expression code for phone number in PHP. Thanks.   PHP - regular expression code for phone number \+?([0-9]{2})-?([0-9]{3})-?([0-9]{6,7
JSPs : Expressions
JSPs : Expressions This tutorial contains description of Expressions which is one of JSP page element. Expressions : Expressions in JSPs is used... for scriptlets. Expressions are checked at run time so it has access to all of the JSP
A program in Java to evaluate APL expressions.
A program in Java to evaluate APL expressions.  APL is a programming language in which arithmetic/mathematical expressions are evaluated right... in Java to evaluate APL expressions. (Hint the expression is read as a string
java regular expression
java regular expression  hi i need java expression which allows all characters with minimum 2 characters and maximum 20 characters in answers field after selecting security questions
Regular expression in c++{MFC}
Regular expression in c++{MFC}  Hi ALL, I want to build a regular expression in c++{MFC} which validates the URL. The regular expression must... of %5C & %2F character. How can we develop a generic Regular Expression
Regular expression contains character - PHP
Regular expression contains character  A PHP script that can check if the regular expression contains character
QuickREx
; Regular Expressions are powerful but can be confusing at times. I regularly found myself working with scrapbook-pages to test my regular expressions... and to be able to play with regular expressions more easily. QuickREx gives you a view
Regular expression in PHP for email - PHP
Regular expression in PHP for email  can any one tell me how to write regular expression in PHP for email to retrieve all the valid email ID's from the email
URL Regular Expression for Struts Validator
URL Regular Expression for Struts Validator  I want to know how to validate a url link in struts validation.xml. Please share me the regular expression for url link validation
URL Regular Expression for Struts Validator
URL Regular Expression for Struts Validator  I want to know how to validate a url link in struts validation.xml. Please share me the regular expression for url link validation
ModuleNotFoundError: No module named 'djorm-ext-expressions'
ModuleNotFoundError: No module named 'djorm-ext-expressions'  Hi...: No module named 'djorm-ext-expressions' How to remove the ModuleNotFoundError: No module named 'djorm-ext-expressions' error? Thanks   Hi
ModuleNotFoundError: No module named 'cucumber-tag-expressions'
ModuleNotFoundError: No module named 'cucumber-tag-expressions'  Hi...: No module named 'cucumber-tag-expressions' How to remove the ModuleNotFoundError: No module named 'cucumber-tag-expressions' error? Thanks  
ModuleNotFoundError: No module named 'cucumber-tag-expressions'
ModuleNotFoundError: No module named 'cucumber-tag-expressions'  Hi...: No module named 'cucumber-tag-expressions' How to remove the ModuleNotFoundError: No module named 'cucumber-tag-expressions' error? Thanks  
ModuleNotFoundError: No module named 'django-pgcrypto-expressions'
ModuleNotFoundError: No module named 'django-pgcrypto-expressions'  ...: No module named 'django-pgcrypto-expressions' How to remove the ModuleNotFoundError: No module named 'django-pgcrypto-expressions' error? Thanks
Version of com.laamella>s-expressions-parent dependency
List of Version of com.laamella>s-expressions-parent dependency
PHPMYSQL REGULAR EXPRESSION - WebSevices
PHPMYSQL REGULAR EXPRESSION  Hello all, I have to read a file and insert the match into mysql.File has a follwing lines. user196 Auth-Type := Local, User-Password == mypass@wd Session-Timeout = 3600
Maven Dependency s-expressions >> 0.1
You should include the dependency code given in this page to add Maven Dependency of com.laamella >> s-expressions version0.1 in your project

Ads