无法使这些正则表达式匹配正常工作。我该怎么做?

huangapple go评论53阅读模式
英文:

Cannot get these regex matches to work correctly. How do I do this?

问题

我正试图让一个程序在做作业时运行。我已经为 GUI 中显示的所有其他代码编写好了,但我们需要使用正则表达式对输入进行检查,看看它们是否匹配。我需要检查并确保有 6 个介于 0 到 99 之间的数字,它们之间都用一个或多个空格分隔,字符串的开头和结尾没有空格。

在 inputValidation 方法中,我正在进行这些检查。以下是我到目前为止的代码。

public String computeSum(String numbers) {
    String errorMessage = validateInput(numbers);
    if (!errorMessage.isEmpty()) {
        return errorMessage;
    }
    int sum = 0;
    String[] nums = numbers.split("\\s+");
    for (int i = 0; i < nums.length; i++) {
        sum += Integer.parseInt(nums[i]);
    }
    return "" + sum;
}

public String validateInput(String numbers) {
    // 检查输入是否为空
    if (numbers == null || numbers.isEmpty()) {
        return "错误:输入不能为空";
    }

    // 检查第一个数字之前或最后一个数字之后是否有空格
    if (!numbers.matches("^(?!\\s)\\d{1,2}( \\d{1,2})*(?!\\s)$")) {
        return "错误:第一个数字之前或最后一个数字之后不能有空格";
    }
    // 检查所有数字是否介于 0 到 99 之间(含)
    else if (!numbers.matches("^(\\d{1,2} ){5}\\d{1,2}$")) {
        return "错误:所有数字必须介于 0 到 99 之间(含)";
    } else {
        return "";
    }
}

任何帮助都将不胜感激。

我不太清楚为什么我的正则表达式没有被正确检测到。目前,“检查第一个数字之前或最后一个数字之后是否有空格”检查是正确的,但如果我有一个大于 99(含)的数字,它会触发相同的错误消息,而不是我特定的检查。

英文:

I am attempting to make a program run for homework. I have all of the other code written for the gui that everything is displayed in, but we are required to do checks on the input by using Regex's and going to see if they match. I need to check and make sure that there are 6 numbers 0-99 inclusive that are all separated by 1 or more spaces with no spaces at the beginning or end of the string.

The inputValidation method is where I am doing these checks. This is the code that I have so far.

    public String computeSum(String numbers) {
        String errorMessage = validateInput(numbers);
        if (!errorMessage.isEmpty()) {
            return errorMessage;
        }
        int sum = 0;
        String[] nums = numbers.split(&quot;\s+&quot;);
        for (int i = 0; i &lt; nums.length; i++) {
            sum += Integer.parseInt(nums[i]);
        }
        return &quot;&quot; + sum;
    }

    public String validateInput(String numbers) {
        // Check if the input is empty
        if (numbers == null || numbers.isEmpty()) {
            return &quot;Error: Input cannot be empty&quot;;
        }

        // Check if there are any spaces before the first number or after the last number
        if (!numbers.matches(&quot;^(?!\\s)\\d{1,2}( \\d{1,2})*(?!\\s)$&quot;)) {
            return &quot;Error: Cannot have spaces before the first number or after the last number&quot;;
        }
        // Check if all numbers are between 0 and 99 inclusive
        else if (!numbers.matches(&quot;^(\\d{1,2} ){5}\\d{1,2}$&quot;)) {
            return &quot;Error: All numbers must be between 0 and 99 inclusive&quot;;
        }
        else
        {
         return &quot;&quot;;   
        }

Any help would be appreciated.

I don't really know why my regex's aren't being detected correctly. Currently the "Check if there are any spaces before the first number or after the last number" check is going off correctly, but if I have a number that is greater than 99 inclusive, it shoots the same error message rather than the correct one with the specific check that I made.

答案1

得分: 1

以下是您要翻译的代码部分:

public static String validateInput1(String numbers) {
	// Check if the input is empty
	if (numbers == null || numbers.isEmpty()) {
		return "Error: Input cannot be empty";
	}

	// Check if there are any spaces before the first number or after the last number
	if (numbers.matches("^\\s+\\d{1,2}(\\s+\\d{1,2}){5}$") 
			|| numbers.matches("^\\d{1,2}(\\s+\\d{1,2}){5}\\s+$")
			|| numbers.matches("^\\s+\\d{1,2}(\\s+\\d{1,2}){5}\\s+$")) {
		return "Error: Cannot have spaces before the first number or after the last number";
	}
	// Check if all numbers are between 0 and 99 inclusive
	else if (!numbers.matches("^\\d{1,2}(\\s+\\d{1,2}){5}$")) {
		return "Error: All numbers must be between 0 and 99 inclusive";
	} else {
		return "";
	}
}

原因:
对于“检查第一个数字之前或最后一个数字之后是否有空格”,您使用了if (!numbers.matches("^\\s*\\d{1,2}(\\s+\\d{1,2})*$")),但这会返回false,因为没有捕获任何内容,它返回false并始终给出“错误:第一个数字之前或最后一个数字之后不能有空格”。第二个条件(!numbers.matches("^(\\d{1,2} ){5}\\d{1,2}$"))工作正常,但是numbers.matches("^(\\d{1,2} ){5}\\d{1,2}$")对于两个数字之间的多个空格会返回false,从而导致“错误:所有数字必须在0到99之间包括”。另外,.matches(String regex)匹配整个字符串,而不是其中的一部分,所以我使用了以下代码进行空格检查:

numbers.matches("^\\s+\\d{1,2}(\\s+\\d{1,2}){5}$") || numbers.matches("^\\d{1,2}(\\s+\\d{1,2}){5}\\s+$") || numbers.matches("^\\s+\\d{1,2}(\\s+\\d{1,2}){5}\\s+$")

请使用上面的代码。

英文:

Use this code:

public static String validateInput1(String numbers) {
	// Check if the input is emptyXj
	if (numbers == null || numbers.isEmpty()) {
		return &quot;Error: Input cannot be empty&quot;;
	}

	// Check if there are any spaces before the first number or after the last
	// number
	if (numbers.matches(&quot;^\\s+\\d{1,2}(\\s+\\d{1,2}){5}$&quot;) 
			|| numbers.matches(&quot;^\\d{1,2}(\\s+\\d{1,2}){5}\\s+$&quot;)
			|| numbers.matches(&quot;^\\s+\\d{1,2}(\\s+\\d{1,2}){5}\\s+$&quot;)) {
		return &quot;Error: Cannot have spaces before the first number or after the last number&quot;;
	}
	// Check if all numbers are between 0 and 99 inclusive
	else if (!numbers.matches(&quot;^\\d{1,2}(\\s+\\d{1,2}){5}$&quot;)) {
		return &quot;Error: All numbers must be between 0 and 99 inclusive&quot;;
	} else {
		return &quot;&quot;;
	}
}

Reason
For the "Check if there are any spaces before the first number or after the last number", you used if (!numbers.matches(&quot;^(?!\\s)\\d{1,2}( \\d{1,2})*(?!\\s)$&quot;))

  • First numbers.matches(&quot;^(?!\\s)\\d{1,2}( \\d{1,2})*(?!\\s)$&quot;)
    returns false because nothing is captured and as nothing gets captured it returns false and always gives Error: Cannot have spaces before the first number or after the last number
  • Second (!numbers.matches(&quot;^(\\d{1,2} ){5}\\d{1,2}$&quot;)) is working fine but numbers.matches(&quot;^(\\d{1,2} ){5}\\d{1,2}$&quot;) will return as false for any 2 numbers seperated by more than one space which will give Error: All numbers must be between 0 and 99 inclusive

Also .matches(String regex) matches the whole string not a part of it so for the space check I have used

numbers.matches(&quot;^\\s+\\d{1,2}(\\s+\\d{1,2}){5}$&quot;) || numbers.matches(&quot;^\\d{1,2}(\\s+\\d{1,2}){5}\\s+$&quot;) || numbers.matches(&quot;^\\s+\\d{1,2}(\\s+\\d{1,2}){5}\\s+$&quot;)

So use the above code.

huangapple
  • 本文由 发表于 2023年2月6日 10:42:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356917.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定