Java Regex Challenge - 1

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 20623 Views - Last Post: 16 February 2012 - 08:18 AM

#1 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Java Regex Challenge - 1

Post icon  Posted 17 February 2011 - 06:31 AM

*
POPULAR

Java Regex Challenge


Inspired by this post has lead to this challenge in the world of regular expressions.

Regular expressions is used for pattern matching certain strings such as validating emails and passwords and is used within many languages apart from Java.

See Regular Expression - Wiki and Regular Expressions - Oracle for more information.

An example of this could be seeing if a string passed is a digit.
public static boolean isInteger(String str) {

	return str.matches("[0-9]+");
}


isInteger() method checks whether a string contains only digits.

[0-9] matches for digits across the String and + uses a Greedy Quantifier to see if it contains 0-9 digits once or more.

This test case System.out.println(isInteger("444345")); would return true.
This test case System.out.println(isInteger("444dfg345")); would return false.

Here's the first regex challenge.

Easy

Validate a password so it contains only alphanumerical characters and must have a length of 8 characters or more.

Hard

Validate a password so it contains only alphanumerical characters and must have a length of between 8 characters - 16 characters, atleast 1 uppercase character and must contain two or more digits.

It must be a method that returns a boolean checking if the String matches the regular expression pattern.

Heres a method template

	public static boolean isValidPassword(String str) {

		return FILL THIS BIT
	}



Bonus marks for short regular expressions and using the matches() method instead of the Pattern class.

This post has been edited by m-e-g-a-z: 17 February 2011 - 08:23 AM


Is This A Good Question/Topic? 5
  • +

Replies To: Java Regex Challenge - 1

#2 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: Java Regex Challenge - 1

Posted 17 February 2011 - 07:13 AM

For this exercise it doesn't matter, but the String#matches() method is less efficient that using compiled patterns. If you're building a production app that does regex matching repeatedly at runtime, you should compile your patterns first. You won't get any bonus points for doing so, but your app will be more efficient.

// compile once at startup
private static final Pattern ZIP_PATTERN = Pattern.compile("^\\d{5}([\\-]\\d{4})?$");

...

// reuse the compiled pattern throughout the app
ZIP_PATTERN.matcher("92154").matches()



// This compiles the pattern each time it's called
"92154".matches("^\\d{5}([\\-]\\d{4})?$")

/* Look into the source and you'll see what's happening.  
 * It calls Pattern.matches("^\\d{5}([\\-]\\d{4})?$", "92154"); 
 * which in turn does this:
 */ 
Pattern p = Pattern.compile("^\\d{5}([\\-]\\d{4})?$");
Matcher m = p.matcher("92154");
return m.matches();

Was This Post Helpful? 2
  • +
  • -

#3 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: Java Regex Challenge - 1

Posted 17 February 2011 - 07:13 AM

Spoiler

This post has been edited by masijade: 17 February 2011 - 11:51 AM

Was This Post Helpful? 2
  • +
  • -

#4 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Java Regex Challenge - 1

Posted 17 February 2011 - 07:36 AM

@masijade, Would be shorter if you used a positive lookahead.

This is my hard challenge solution.
Spoiler


Edit - add code tags in spoiler.

This post has been edited by m-e-g-a-z: 17 February 2011 - 08:25 AM

Was This Post Helpful? 2
  • +
  • -

#5 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Java Regex Challenge - 1

Posted 17 February 2011 - 08:36 AM

Shorter version
Spoiler

Was This Post Helpful? 0
  • +
  • -

#6 madblogr  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 29-March 09

Re: Java Regex Challenge - 1

Posted 17 February 2011 - 08:41 AM

Here is my attempt at the hard challenge:

Spoiler

Was This Post Helpful? 0
  • +
  • -

#7 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Java Regex Challenge - 1

Posted 17 February 2011 - 08:46 AM

@madblogr, you need to add an extra backslash since '\d' is an escape character.

This

Quote

adjj2F2df6d
returns false.
Was This Post Helpful? 0
  • +
  • -

#8 jumptrooper  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 68
  • View blog
  • Posts: 234
  • Joined: 19-August 10

Re: Java Regex Challenge - 1

Posted 17 February 2011 - 11:33 AM

Here you go:
Spoiler


oh wait! CRAP! I didn't read all the requirements!

This post has been edited by jumptrooper: 17 February 2011 - 11:34 AM

Was This Post Helpful? 0
  • +
  • -

#9 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: Java Regex Challenge - 1

Posted 17 February 2011 - 11:52 AM

View Postm-e-g-a-z, on 17 February 2011 - 03:36 PM, said:

@masijade, Would be shorter if you used a positive lookahead.


I know. ;-) I only had about 5 minutes time to work on it, though. ;-)
Was This Post Helpful? 0
  • +
  • -

#10 jumptrooper  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 68
  • View blog
  • Posts: 234
  • Joined: 19-August 10

Re: Java Regex Challenge - 1

Posted 17 February 2011 - 12:11 PM

Haha! I return victorious! (I hope)
Spoiler

Was This Post Helpful? 1
  • +
  • -

#11 madblogr  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 29-March 09

Re: Java Regex Challenge - 1

Posted 17 February 2011 - 12:49 PM

View Postm-e-g-a-z, on 17 February 2011 - 10:46 AM, said:

@madblogr, you need to add an extra backslash since '\d' is an escape character.

This

Quote

adjj2F2df6d
returns false.


Spoiler


Shortened it by removing the check for lowercase as well since it wasn't a requirement and since it is checking for a password, I don't think the ^ and $ anchors are required as in m-e-g-a-z solution. It could be further shortened by two characters if punctuation was allowed:

Spoiler

Was This Post Helpful? 0
  • +
  • -

#12 moobler  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 143
  • View blog
  • Posts: 224
  • Joined: 21-January 11

Re: Java Regex Challenge - 1

Posted 17 February 2011 - 08:59 PM

Now when you say, "must have a length of between 8 characters - 16 characters" does that mean 8 and 16 are valid lengths? Or is it just the values in between: [9-15]?

I'm bored and practicing my TDD, so I'm making up some test cases ;)
Was This Post Helpful? 1
  • +
  • -

#13 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Java Regex Challenge - 1

Posted 18 February 2011 - 01:08 AM

It means 8 to 16 are valid, anything higher and lower is invalid.

So the valid lengths would be 8,9,10,11,12,13,14,15 and 16. :)
Was This Post Helpful? 0
  • +
  • -

#14 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Java Regex Challenge - 1

Posted 18 February 2011 - 08:36 AM

Thanks to moobler for getting some test cases done so I could use JUnit for some testing :^:.

I can now say jumptrooper's solution was correct. :^: 39 Characters.

Although this is the shortest I could get.

^(?=.*\\d.*\\d)(?=.*[A-Z])\\w{8,16}$ 33 Characters.
Was This Post Helpful? 0
  • +
  • -

#15 moy2414  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 18
  • Joined: 25-January 12

Re: Java Regex Challenge - 1

Posted 29 January 2012 - 02:12 PM

Here is my first attempt. Looking at everyone else's makes mine seem very amateur haha but atleast mine works.

Easy
public static boolean isValid (String password)
    {
        if(password.length()<8)
            return false;
        else if(!password.matches("[0-9a-zA-Z]+"))
            return false;
        else
            return true;
    }



Hard
public static boolean isValid2 (String password)
    {
        if(password.length()<8 || password.length()>16)
            return false;
        else if(!password.matches("[0-9a-zA-Z]+"))
            return false;
        else
        {
            int numCount=0;
            int capitalCount=0;
            for(int i=0; i<password.length(); i++)
            {
                if(Character.isDigit(password.charAt(i)))
                    numCount++;
                else if(Character.isLetter(password.charAt(i)) && (password.charAt(i)+"").matches("[A-Z]"))
                        capitalCount++;
            }
            
            if(numCount>=2 && capitalCount>=1)
                return true;
            else
                return false;
        }
    }


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2