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

New Topic/Question
Reply




MultiQuote








|