Welcome, Guest. Please login or register.
Did you miss your activation email?
Pages: [1]   Go Down
  Print  
Author Topic: Check for valid postcode using regex  (Read 1000 times)
0 Members and 1 Guest are viewing this topic.
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2593
Referrals: 13

WWW Awards
« on: April 26, 2009, 08:08:16 AM »

Code
GeSHi (java):
  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3.  
  4. class RegexString {
  5. public static void main(String[] args){
  6. String txt="1234AB";
  7. String re1="^[0-9]{4}[a-z|A-Z]{2}$";
  8.  
  9. Pattern p = Pattern.compile(re1);
  10. Matcher m = p.matcher(txt);
  11. if (m.matches()){
  12. String text=m.group(0);
  13. System.out.print("Postcode: "+text.toString());
  14. }
  15. if (!m.matches()){
  16. System.out.print("Not a valid postcode.");
  17. }
  18. }
  19. }
  20.  
Created by GeSHI 1.0.7.20
Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
Pages: [1]   Go Up
  Print  
 
Jump to:  

Your Ad Here