So, apparently in Java you need to declare a
Pattern type based on the string that contains the regular expression.
You have to "compile" this Pattern in order to use it with a
Matcher. The Matcher is the thing that will compare the String to the Regular Expression pattern.
So, you need to declare a Pattern based on a string that contains regular expression pattern that you want to check. And you need to "compile" that Pattern. Then you need to declare a new Matcher based on the compiled Pattern.
Like this:
-
Pattern p = Pattern.compile("^SB2011[0-9]{4}$");
-
Matcher m = p.matcher(str); //str is the String that needs validating
Now you can call the Matcher's "matches()" method to see if the string you want to validate "matches" the pattern that you are expecting:
-
public boolean ValidateAccountNumber(string str)
-
{
-
Pattern p = Pattern.compile("^SB2011[0-9]{4}$");
-
Matcher m = p.matcher(str); //str is the String that needs validating
-
return m.matches();
-
}
Pretty clean isn't it :)
-Frinny