Welcome, Guest. Please login or register.
Did you miss your activation email?
Your Ad Here
Pages: [1]   Go Down
  Print  
Author Topic: Regex validation help  (Read 1525 times)
0 Members and 1 Guest are viewing this topic.
deAppel
Favorite member
Member
*

Reputation: 0
HappyFace plants one on you
Offline Offline
Posts: 45
Referrals: 0
Activity
0%

Awards
« on: December 03, 2007, 08:42:48 AM »

Hi,

I'm new with regex and i wanted to ask how to properly validate a string using regex?

I created a small demo here:
Code
GeSHi (java):
  1. class RegexString {
  2. public static void main(String[] args){
  3. String txt="abCD";
  4. String re1="[a-zA-Z]*";
  5. Pattern p = Pattern.compile(re1,Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
  6.  
  7. Matcher m = p.matcher(txt);
  8. if (m.find()){
  9. String text=m.group(0);
  10. System.out.print("String: "+text.toString());
  11. }
  12. else{
  13. System.out.print("Number");
  14. }
  15. }
  16. }
Created by GeSHI 1.0.7.20

Although it prints out the text i can't seem to "catch" a non string value and do something with it. How can i go to the else when the entered text in the txt string does not matches [a-zA-Z] ?

Thanks,

Ark
Logged

Earn $0.04 or more for each search you make! http://www.myhpf.co.uk/apply001.asp?Friend=77889

If you need homework help, just post your assignment and i'll code it before your deadline =) i'll provide you "t3h c0d3z" but only if you show me that you have done something and not just ask for me to write everything for you. Just tell us what the problem is and i'm sure we can fix it for you!

I was born into the Hakka lineage, A bloodline that
traces it's roots back to the original Han emperors of
China. During the rise of the 3 Kingdoms (China's
famous warring period), the Hans were overthrown
and exiled. Forced to flee, they headed south finding
refuge within the mountains of Guilin Province where
they lived under their new title, the Hakka and patiently
wait for their chance to return to the throne as the
Sons Of Heaven...
Javaforums.net/forum :: A community for software developers
« on: December 03, 2007, 08:42:48 AM »

Your Ad Here
 Logged
deAppel
Favorite member
Member
*

Reputation: 0
HappyFace plants one on you
Offline Offline
Posts: 45
Referrals: 0
Activity
0%

Awards
« Reply #1 on: December 03, 2007, 01:55:24 PM »

Nevermind, its fixed. Instead of find() , I was recommended to use matches()

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

Earn $0.04 or more for each search you make! http://www.myhpf.co.uk/apply001.asp?Friend=77889

If you need homework help, just post your assignment and i'll code it before your deadline =) i'll provide you "t3h c0d3z" but only if you show me that you have done something and not just ask for me to write everything for you. Just tell us what the problem is and i'm sure we can fix it for you!

I was born into the Hakka lineage, A bloodline that
traces it's roots back to the original Han emperors of
China. During the rise of the 3 Kingdoms (China's
famous warring period), the Hans were overthrown
and exiled. Forced to flee, they headed south finding
refuge within the mountains of Guilin Province where
they lived under their new title, the Hakka and patiently
wait for their chance to return to the throne as the
Sons Of Heaven...
HappyFace
Javaforums.net admin
Senior Member
*

Reputation: 16
Developer @ Engineeringserver network
Offline Offline
Posts: 2552
Referrals: 12
Activity
14%

WWW Awards
« Reply #2 on: December 04, 2007, 10:36:29 AM »

An email validator using regex:

Code
GeSHi (java):
  1. import java.util.regex.*;
  2.  
  3. class regexSample
  4. {
  5.   public static void main(String args[])
  6.   {
  7.      //Input the string for validation
  8.      String email = "xyz@hotmail.com";
  9.  
  10.      //Set the email pattern string
  11.      Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
  12.  
  13.      //Match the given string with the pattern
  14.      Matcher m = p.matcher(email);
  15.  
  16.      //check whether match is found
  17.      boolean matchFound = m.matches();
  18.  
  19.      if (matchFound)
  20.        System.out.println("Valid Email Id.");
  21.      else
  22.        System.out.println("Invalid Email Id.");
  23.   }
  24. }
Created by GeSHI 1.0.7.20

http://www.devx.com/tips/Tip/28334
Logged

It doesn't matter how hard you've studied; the material won't be on the exam anyway.
Admin @ www.Engineeringserver.com General Admin @ www.Javaforums.net and it's subsites Forum admin @ www.Javaforums.net Global moderator @ www.Engineeringserver.com network Java / .NET blogger @ www.Engineeringserver.com/blog
XMA performer since 2006 @ http://www.engineeringserver.com/XMA-NaN/ Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://www.javaforums.net...lery.html;sa=media;id=170
http://img222.imageshack....707/arkietomatoesmall.jpg
http://img208.imageshack.us/img208/7141/smalli.png
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
Javaforums.net/forum :: A community for software developers
   

Your Ad Here
 Logged
Pages: [1]   Go Up
  Print  
 
Jump to: