Regex Test Error - Java

I am trying to execute a simple regular expression. Essentially, I want to determine if I have special characters in my string, and if so, check each character of the string for two specific characters, for example, hypen and dot.

It seems that I have a problem in the first bit, which includes determining if I have special characters in my string.

Below is my method that I am trying to do, followed by the lines that I'm having problems 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 is the line that I passed to the method, which, as I expected, was considered a line with special characters, but the test failed:

 "QWERTY"!Β£$"Β£$" "sdfGSDFGSDFG%*^(%*&(" 

Below the lines that I passed to the method I was expecting should NOT be considered as strings with special characters, but the test failed:

 "QWE12342134RTY" "LOREMIPSUM2354214" 

Any suggestions are welcome.

+6
source share
2 answers

Running the code using the attached lines gave me the following result:

 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 assume that this means that there is nothing wrong with the pattern you use to find special characters (not numbers or letters). But I found the following problems with your code:

  • Make sure that you pass these strings correctly as parameters. The first line in your list should be declared as "QWERTY \"! Β£ $ \ "Β£ $" in your program when java requires double quotes inside strings that are preceded by a backslash so as not to be interpreted as line separators;
  • The second part of your test does not work, because you only check the first char on your line. Your logic says something like "if the current char is a period or hyphen, specialCharValidity = true, otherwise (in case it is any other invalid OR valid char other than a period and hyphen), just enter specialCharValidity = false and break the loop ". Oddly enough, you already did the right thing: just re-include the lines you commented to indicate the correct invalid char. If you want to enable specials counting, you just need to delete the line with break so that the loop does not stop in the first special;

Some suggestions

  • Replace Character.isSpace() with Character.isWhitespace() as the first version is deprecated already;
  • Define specialCharValidity locally to avoid potential problems;
  • For performance, do not compile the same template for every call you make to the line Pattern p = Pattern.compile("[^a-zA-Z0-9 ]"); . Compiling a template takes a lot of time, so you can simply define a constant on top of your class, for example static public final Pattern p = Pattern.compile("[^a-zA-Z0-9 ]"); , and use it later;
  • Patterns are a great tool for matching complex string patterns, but in this case it’s a bit overkill. If you just need to match / find such characters, you'd better go for a char comparison, as the templates will just add extra service data.
0
source

You can simplify your code by checking the line against the following pattern:

 [^a-zA-Z0-9 \-\.] 

The string confidence function is as follows:

 public static boolean stringValidity(String input) { return Pattern.compile("[^a-zA-Z0-9 \\-\\.]").matcher(input).find() == false; } 
+1
source

Source: https://habr.com/ru/post/979645/


All Articles