Java/Regular Expressions/Match Address — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 06:01, 1 июня 2010
Match Address: mixing numbers and characters
public class Main {
public static void main(String args[]) {
String addr = "street 124 a0a";
String nameToken = "\\p{Upper}(\\p{Lower}+\\s?)";
String namePattern = "(" + nameToken + "){2,3}";
String zipCodePattern = "\\d{5}(-\\d{4})?";
String addressPattern = "^" + namePattern + "\\w+ .*, \\w+ " + zipCodePattern + "$";
System.out.println(addr.matches(addressPattern));
}
}
Match address regular expressions
public class MatchAddress {
public static void main(String args[]) {
isAddressValid("John Smith 888 Luck Street,NY 64332");
isAddressValid("John A. Smith 888 Luck Street, NY 64332-4453");
isAddressValid("John Allen Smith 888 Luck Street, NY 64332-4453");
isAddressValid("888 Luck Street, NY 64332");
isAddressValid("P.O. BOX 888 Luck Street, NY 64332-4453");
isAddressValid("John Allen Smith 888 Luck st., NY");
}
public static boolean isAddressValid(String addr) {
boolean retval = false;
String nameToken = "\\p{Upper}(\\p{Lower}+\\s?)";
String namePattern = "(" + nameToken + "){2,3}";
String zipCodePattern = "\\d{5}(-\\d{4})?";
String addressPattern = "^" + namePattern + "\\w+ .*, \\w+ "
+ zipCodePattern + "$";
retval = addr.matches(addressPattern);
String msg = "NO MATCH\npattern:\n " + addr + "\nregexLength:\n "
+ addressPattern;
if (retval) {
msg = "MATCH\npattern:\n " + addr + "\nregexLength:\n "
+ addressPattern;
}
System.out.println(msg + "\r\n");
return retval;
}
}
Match Email address
/**
* SubStringDemo.java separates domain name like "@yahoo.ru"
* from email id like "suraj.gupta@yahoo.ru"
*
*/
public class SubStringDemo {
/**
* @author suraj.gupta
*/
public static void main(String[] args) {
String s = "suraj.gupta@yahoo.ru"; // email id in a String
int IndexOf = s.indexOf("@"); // returns an integer which tells the position of this substring "@" in the parent String "suraj.gupta@yahoo.ru"
String domainName = s.substring(IndexOf); //prints the String after that index
System.out.println("Taking Domain name from an email id "+domainName);
}
}