Reputation: 771
I'm attempting a simple regex execution. Essentially I want to determine if I've got special characters in my string and if so check each character of the string for two specific characters i.e. hypen and dot.
I seem to be having a problem in the first bit which involves determining if I've got special characters in my string.
Below is my method which I attempt to do this followed by the strings I'm having issues with:
public static boolean stringValidity(String input) {
int specials = 0;
Pattern p = Pattern.compile("[^a-zA-Z0-9 ]");
Matcher m = p.matcher(input);
boolean b = m.find();
if (b) {
System.out.println("\nstringValidity - There is a special character in my string");
for (int i = 0; i < input.length(); ++i) {
char ch = input.charAt(i);
//if (!Character.isDigit(ch) && !Character.isLetter(ch) && !Character.isSpace(ch)) {
++specials;
System.out.println("\nstringValidity - Latest number of special characters is: " + specials);
if((ch == '-') | (ch == '.')) {
specialCharValidity = true;
System.out.println("\nstringValidity - CHAR is valid - specialCharValidity is: " + specialCharValidity + " as char is: " + ch);
} else {
specialCharValidity = false;
System.out.println("\nstringValidity - CHAR is invalid - specialCharValidity is: " + specialCharValidity + " as char is: " + ch);
break;
}
//}
}
} else {
System.out.println("\nstringValidity - There is NO special character in my string");
specialCharValidity = true;
}
return specialCharValidity;
}
Below are strings I passed to the method which I expected to be considered as strings with special characters but the test failed:
"QWERTY"!£$"£$"
"sdfGSDFGSDFG%*^(%*&("
Below are strings I passed to the method which I expected NOT to be considered as strings with special characters but the test failed:
"QWE12342134RTY"
"LOREMIPSUM2354214"
Any suggestions are appreciated.
Upvotes: 6
Views: 330
Reputation: 999
Running your code with the supplied strings gave me the following output:
stringValidity - There is a special character in my string
stringValidity - Latest number of special characters is: 1
stringValidity - CHAR is invalid - specialCharValidity is: false as char is: Q
---
stringValidity - There is a special character in my string
stringValidity - Latest number of special characters is: 1
stringValidity - CHAR is invalid - specialCharValidity is: false as char is: s
---
stringValidity - There is NO special character in my string
---
stringValidity - There is NO special character in my string
---
I guess this means there is nothing wrong with the pattern you are using to find special chars (not digits nor letters). But I've found the following issues with your code:
specials
you just need to remove line with break
so the loop wont stop in the first special; A few suggestions
Character.isSpace()
with Character.isWhitespace()
as the first version is deprecated already;specialCharValidity
locally to avoid potential problems;Pattern p = Pattern.compile("[^a-zA-Z0-9 ]");
. Compiling a pattern is time consuming so you can just define a constant on top of your class like static public final Pattern p = Pattern.compile("[^a-zA-Z0-9 ]");
and use it later;Upvotes: 0
Reputation: 272236
You can simplify your code by checking the string against the following pattern:
[^a-zA-Z0-9 \-\.]
The string validity function boils down to:
public static boolean stringValidity(String input)
{
return Pattern.compile("[^a-zA-Z0-9 \\-\\.]").matcher(input).find() == false;
}
Upvotes: 1