简体   繁体   English

正则表达式测试失败 - Java

[英]Regex test failing - Java

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 ie hypen and dot. 基本上我想确定我的字符串中是否有特殊字符,如果是,请检查字符串的每个字符是否有两个特定字符,即hypen和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. 任何建议表示赞赏。

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;
}

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: 但我发现您的代码存在以下问题:

  1. Make sure you are properly passing those strings as parameters. 确保您正确地将这些字符串作为参数传递。 The first string in your list should be declared like this "QWERTY\\"!£$\\"£$" in your program once java requires double quotes inside strings to be preceded by a back slash in order not to be interpreted as string delimiters; 列表中的第一个字符串应该在程序中声明为“QWERTY \\”!£$ \\“£$”一旦java要求字符串中的双引号前面加上反斜杠,以免被解释为字符串分隔符;
  2. The second part of your test is not working because you are only testing the first char in your string. 测试的第二部分无法正常工作,因为您只测试字符串中的第一个字符。 Your logic is saying something like "if the current char is a dot or an hyphen, specialCharValidity = true, otherwise (in case it is any other invalid OR valid char other than a dot and an hyphen) just make specialCharValidity = false and break the loop". 你的逻辑说的是“如果当前的char是一个点或连字符,specialCharValidity = true,否则(如果它是除了点和连字符之外的任何其他无效的OR有效字符),只需使specialCharValidity = false并打破环”。 Oddly, you have already done the right thing: just re-enable the lines you've commented to list the proper invalid char. 奇怪的是,您已经做了正确的事情:只需重新启用您评论的行以列出正确的无效字符。 If you want to enable the counting of specials you just need to remove line with break so the loop wont stop in the first special; 如果你想启用specials计数,你只需要删除一行break这样循环就不会在第一个特殊情况下停止;

A few suggestions 一些建议

  • Replace Character.isSpace() with Character.isWhitespace() as the first version is deprecated already; 更换Character.isSpace()Character.isWhitespace()作为第一个版本已经过时;
  • Define specialCharValidity locally to avoid potential problems; 在本地定义specialCharValidity以避免潜在的问题;
  • For the sake of performance, do not compile the same pattern on every call like you are doing on the line Pattern p = Pattern.compile("[^a-zA-Z0-9 ]"); 为了提高性能,不要像在线Pattern p = Pattern.compile("[^a-zA-Z0-9 ]");那样在每个调用上编译相同的模式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 ]"); 编译模式非常耗时,因此您可以在类的顶部定义常量,如static public final Pattern p = Pattern.compile("[^a-zA-Z0-9 ]"); and use it later; 并在以后使用它;
  • Patterns are an excellent tool to match complex string patterns but are a little overkill in this case. 模式是匹配复杂字符串模式的绝佳工具,但在这种情况下有点过分。 If you just need to match/find chars like this you'd better go for char comparison alone as patterns would just add unnecessary overhead. 如果你只需要匹配/找到这样的字符,你最好单独进行字符比较,因为模式会增加不必要的开销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM