|
| |||||||||
| Tags: date, formate, java tools, javautil, regular expression, utilities |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| Regex to validate a date
Hello, I have stuck with a regular expression pattern in java. The aim is to validate email addresses. I have tried searching on internet but what I could not find helpful information and compiled so far do not give good results in both cases following Here is my code: Code: Pattern p =.compiles("(\\w +) @ (\\w +\\.)(\\w +) (\\.\\w +) * ");
Matcher m = p.matcher(input);
if (! m.find()){
System.out.System.out.println("Email address has wrong format! Expected: xxx@xxx.xx (. Xx)>");
}else{
System.out.System.out.println("OK");
} |
|
#2
| |||
| |||
| Re: Regex to validate a date
Hi, You used the wrong class Matcher : Method find () finds the first sub-string that matches your pattern. To verify that the entire string matches the pattern we must use the method matches (). Then he would perhaps see your pattern ... accept emails underscore and dash, as the points. The following emails are valid but not with your expressions: Here is the code. Code: "firstnm.lastname @ test.com" "example@test.com" "example@test.com" |
|
#3
| |||
| |||
| Re: Regex to validate a date
Hello, Do not use the method find() Which seeks an instance of the regular expression but the method games() Which it checks the consistency right. You can even use the direct method games() The String class (this method is that of the Matcher class but gives the same result). Just look at the following code. Code: Pattern p =.compiles("\\w + @\\w +\\.\\w + (\\.\\w +) * ");
Matcher m = p.matcher(input);
if(m.games() {
/ / OK
} else {
/ / Invalid Email
} |
|
#4
| |||
| |||
| Re: Regex to validate a date
Hello, I think you can also use the following code instead of what you are using in the code. Code: if(in.games("\\w + @\\w +\\.\\w + (\\.\\w +) * ")) {
/ / OK
} else {
/ / Invalid Email
} |
|
#5
| |||
| |||
| Re: Regex to validate a date
Hello, This is my update code, please have a look at it, if you think that there is a correction to me made in the code then please guide me with the same. Code: Public static boolean isEmailWellFormed(String email){
String in = email;
if (in == null){
System.out.System.out.println("Email address can not be null!");
return false;
}
/ / regex pattern info: http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html
/ / checks for email right format
Pattern Pattern p =.compiles("(\\w +) ((\\.|\\-)\\w +)*@((\\w +\\.)|(\\w +\\-))(\\w +) (\\.\\w +) * ");
Matcher m = p.matcher(in);
if (! m.games()) {
System.out.System.out.println("Email address is not correct!");
return false;
}
return true;
} |
|
#6
| |||
| |||
| Re: Regex to validate a date
Hello, For the Junit class you can use the following, if in case you are using it in your code. Code: @ Test
Public void emailIsNull(){
assertFalse(Validator.isEmailWellFormed(null));
}
@ Test
Public void emailIsEmpty(){
assertFalse(Validator.isEmailWellFormed(""));
}
@ Test
Public void emailStartsWithDot(){
assertFalse(Validator.isEmailWellFormed(. @ test.com "));
}
@ Test
Public void emailStartsWithArobase(){
assertFalse(Validator.isEmailWellFormed("@ test.com"));
}
@ Test
Public void emailWithoutArobase(){
assertFalse(Validator.isEmailWellFormed("test.com"));
}
@ Test
Public void emailContains2Arobases(){
assertFalse(Validator.isEmailWellFormed("test @ test@test.com"));
}
@ Test
Public void emailContainsIllegalChar(){
char c = ']';
assertFalse(Validator.isEmailWellFormed("test" C + + "test @ test."));
}
@ Test
Public void emailBadFormat1(){
assertFalse(Validator.isEmailWellFormed("test @ test"));
}
@ Test
Public void emailBadFormat2(){
assertFalse(Validator.isEmailWellFormed("test@test.com test@test.com"));
}
@ Test
Public void emrgfrm1(){
assertTrue(Validator.isEmailWellFormed("test@test.com"));
}
@ Test
Public void emrgfrm2(){
assertTrue(Validator.isEmailWellFormed("test_test@test.com"));
}
@ Test
Public void emrgfrm3(){
assertTrue(Validator.isEmailWellFormed("test.test @ test.com"));
}
@ Test
Public void emrgfrm4(){
assertTrue(Validator.isEmailWellFormed("test-test@test.com"));
}
@ Test
Public void emrgfrm5(){
assertTrue(Validator.isEmailWellFormed("test@test_test.ch"));
}
@ Test
Public void emrgfrm6(){
assertTrue(Validator.isEmailWellFormed("test@test.test.ch"));
}
@ Test
Public void emrgfrm7(){
assertTrue(Validator.isEmailWellFormed("test@test-test.ch"));
} |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "Regex to validate a date" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| PHP REGEX to Validate Domain Name | Xmen | Software Development | 6 | 13-05-2010 05:17 PM |
| Regex with xml tag | Vodka | Software Development | 5 | 04-02-2010 02:11 AM |
| How to Validate XML Against a DTD? | PsYcHo 1 | Software Development | 5 | 02-02-2010 12:33 AM |
| Query in php regex | Linux-Us | Software Development | 5 | 14-12-2009 12:54 PM |
| WM 11 won't validate | Buckwheat | Media Player | 3 | 02-03-2007 04:43 AM |