Results 1 to 8 of 8
  1. #1
    milky is offline Member
    Join Date
    Jun 2016
    Posts
    4
    Rep Power
    0

    Default Regular Expression for checking Credit Card Numbers

    Hello, I need to formulate a Regular Expression for a credit card formatted "XXXXXXXXXXXX0209". So the string must have 12 "X's" and 4 numbers. So far I have:

    \w [X,12] \d [0-9,4]

    Unfortunately that's not working, I'm new to Java.

    Thank you!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Regular Expression for checking Credit Card Numbers

    that's not working, I'm new to Java.
    Regular expressions are not really part of java. I don't know where there is a good site to ask questions about regular expressions.
    There are probably some people on this forum that know more about them.

    Could you post a short, simple, complete program that shows what you are trying to do?
    Be sure to wrap the code in code tags.

    If there are any error messages, copy the full text and paste it here.
    Last edited by Norm; 06-20-2016 at 06:50 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Regular Expression for checking Credit Card Numbers

    You can test regular expressions at Regex Tester - Javascript, PCRE, PHP. However, the regex engine in Java is different than say the one in Perl. Others
    may be different than those. Are you planning on using Java for your application? If you are using GUI's you may want to consider a formatted
    text box. Check the tutorials in my signature and click on big index to find the tutorial.

    As I am familiar with both Perl and Java regex, let me know if you need further assistance.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    183
    Rep Power
    8

    Default Re: Regular Expression for checking Credit Card Numbers

    A very good site about regular expressions is : Java - Regular Expressions
    So if I understand you correct you want to have a string with the this format "XXXXXXXXXXXXNNNN" Where X = 'X' character and the last four are numbers ( N = number)

    Below there is an example of 3 similar formats to give you a better understanding

    Java Code:
    import java.util.Scanner;
    
    public class RegenTest {
    	
    	public static void main(String[] args) {
    		// Scanner to get inputs from terminal
    		Scanner input = new Scanner(System.in);
    		
    		// our string with the correct format
    		String formatedString = "";
    		
    		// We will keep asking for the user to enter a value for the string
    		// as long as the method 'matchRegen' returns false
    		while(!matchRegen(formatedString)){
    			System.out.print("Give string with correct format :");
    			// Get input
    			formatedString = input.nextLine();
    		}
    		// If we escape from while we have the correct format
    		System.out.println("Given string is :" + formatedString);
    		input.close();
    	}
    
    	// Returns true if the String has one of the above formats
    	//
    	// A. XXXXXXXXXXXXNNNN   where X = 'X' and N = 0...9
    	// B. XXXX-XXXX-XXXX-NNNN    >> >> >> just with '-' between
    	// C. XXXX XXXX XXXX NNNN    >> >> >> just with space between
    	// We separate each situation with '|' character inside regular expression
    	// and the {} is how many times the character on the left it has to have
    	// for example X{2} = XX , X{5} = XXXXX , A{4} = AAAA etc.
    	// keep in mind that leaving spaces also has meaning inside regular expression
    	private static boolean matchRegen(String x) {
    		if(x.matches("X{12}\\d{4}|X{4}-X{4}-X{4}-\\d{4}|X{4} X{4} X{4} \\d{4}")){
    			return true;
    		}else{
    			return false;
    		}
    	}
    }
    Last edited by ShadowWalker; 06-21-2016 at 01:54 AM.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Regular Expression for checking Credit Card Numbers

    Of course, since the value of matches is either true or false, just return that. No need for an if-else.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Join Date
    Jun 2016
    Posts
    7
    Rep Power
    0

    Default Re: Regular Expression for checking Credit Card Numbers

    Quote Originally Posted by jim829 View Post
    You can test regular expressions at Regex Tester - Javascript, PCRE, PHP. However, the regex engine in Java is different than say the one in Perl. Others
    may be different than those. Are you planning on using Java for your application? If you are using GUI's you may want to consider a formatted
    text box. Check the tutorials in my signature and click on big index to find the tutorial.

    As I am familiar with both Perl and Java regex, let me know if you need further assistance.

    Regards,
    Jim
    So you can actually use regular expressions in Java? What about the regular expressions in Rubular, will they work?
    Rubular: a Ruby regular expression editor and tester

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Regular Expression for checking Credit Card Numbers

    I am unfamiliar with Rubular. Regular expressions in Java are not part of the language syntax like they are in Perl. They are
    available via the Pattern class.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    183
    Rep Power
    8

    Default Re: Regular Expression for checking Credit Card Numbers

    Of course, since the value of matches is either true or false, just return that. No need for an if-else.
    Of course :P i just thought it will make my example more readable ;)

Similar Threads

  1. validation checking using regular expression...
    By Debarpita Paul in forum New To Java
    Replies: 4
    Last Post: 01-08-2013, 06:31 PM
  2. Replies: 4
    Last Post: 04-10-2012, 05:48 AM
  3. Seaching a text file for credit card numbers
    By javamann in forum New To Java
    Replies: 9
    Last Post: 11-21-2011, 12:44 AM
  4. Masking a credit card value
    By Samurai Coder in forum New To Java
    Replies: 2
    Last Post: 12-02-2009, 09:05 PM
  5. Credit Card Validator
    By bluegti02 in forum New To Java
    Replies: 2
    Last Post: 06-17-2008, 06:09 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •