Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: validate a String in java Regular Expression

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default validate a String in java Regular Expression

    Dear Friends Kindly help me to validate a String in following way

    a. If first character is an alphabet, input should be Alpha Numeric 24 characters
    b. If first character is a digit, input should be minimum 15 and maximum 16 Numeric characters

    I need a regular Expression or Java program example

    Thanks in advance


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: validate a String in java Regular Expression

    Why?

    You could do this without using regex. If you really need to use regex then get on and write it. It is your job not ours. make an attempt. If you get stuck post your code and ask a specific question.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: validate a String in java Regular Expression

    Dear I made the program but not success ,
    and thanks for suggestion I will consider for next time
    Regards

    --- Update ---

    String string="PK448345678901aSSSsfry35";

    int length=string.length();

    if(string.substring(0, 1).matches("[0-9]")){
    if(length==15 ||length==16){

    System.out.println("First Character is a number..! and Length is "+string.length());
    }
    else{
    throw new InvalidSpeedException("Please Check First Character is Number or check the length");
    }
    }
    if(string.substring(0, 1).matches("[a-zA-Z]")&& matchNum.find()){
    if(matcher.find()&& length==24){
    System.out.println("First character is Character! and Length is "+string.length());
    }
    else{
    System.out.println("Exception");
    throw new InvalidSpeedException("Please Check First is Character or the length");
    }
    }

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: validate a String in java Regular Expression

    Please read the Announcement at the top of the sub-Forums to learn how to post your code in code/formatting tags and how to ask questions that get attention and receive helpful answers.

Similar Threads

  1. Replies: 3
    Last Post: December 22nd, 2011, 09:46 AM
  2. Replies: 3
    Last Post: September 30th, 2011, 08:45 AM
  3. How to remove the regular expression from a string ?
    By srimanta in forum Java Theory & Questions
    Replies: 5
    Last Post: July 20th, 2011, 03:07 PM
  4. Using Regular Expression (regex) in Java Programming
    By lordelf007 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: May 14th, 2010, 10:29 AM
  5. Java program to validate an email address using Regular Expressions
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 07:26 AM