正则表达式以匹配数字和运算符之间的空格,但数字之间没有空格 [英] Regular Expression to match spaces between numbers and operators but no spaces between numbers

查看:395
本文介绍了正则表达式以匹配数字和运算符之间的空格,但数字之间没有空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个论坛上搜索了很多帖子,令我惊讶的是,我还没有发现任何人遇到像我这样的问题。
我必须为控制台创建一个简单的字符串值计算器。现在,我正在尝试制作一些正则表达式来验证输入。
我的计算器必须接受运算符之间带空格的数字(仅允许+和-允许),但不允许数字之间带空格的数字相加:
2 + 2 = 4是正确的,但是
2 2 + 2->这应该会出错,并在控制台上通知用户他在数字之间放置空格。

I've searched many post on this forum and to my surprise, I haven't found anyone with a problem like mine. I have to make a simple calculator for string values from console. Right now,I'm trying to make some regexes to validate the input. My calculator has to accept numbers with spaces between the operators (only + and - is allowed) but not the ones with spaces between numbers, to sum up: 2 + 2 = 4 is correct, but 2 2 + 2 --> this should make an error and inform user on the console that he put space between numbers.

我想出了这个:

static String properExpression = "([0-9]+[+-]?)*[0-9]+$";
static String noInput = ""; 
static String numbersFollowedBySpace = "[0-9]+[\\s]+[0-9]";
static String numbersWithSpaces = "\\d+[+-]\\d+"; 

//I've tried also "[\\d\\s+\\d]";

void validateUserInput() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a calculation.");
input = sc.nextLine();

if(input.matches(properExpression)) {
calculator.calculate();

} else if(input.matches(noInput)) {
    System.out.print(0);

} else if(input.matches(numbersFollowedBySpace)) {
    input.replaceAll(" ", "");
    calculator.calculate();

    } else if(input.matches(numbersWithSpaces)) 
       {
          System.out.println("Check the numbers. 
It seems that there is a space between the digits");
        } 

   else System.out.println("sth else");

您能给我一些有关我应该使用的正则表达式的提示吗?

Can you give me a hint about the regex I should use?

推荐答案

匹配完整的表达式,例如 2 + 3 = 24 6 -4 = 2 ,像

To match a complete expression, like 2+3=24 or 6 - 4 = 2, a regex like

^\d+\s*[+-]\s*\d+\s*=\s*\d+$

会做。查看示例1 ,您可以在其中使用它。

will do. Look at example 1 where you can play with it.

如果要匹配更长的表达式,例如 2 + 3 + 4 + 5 = 14 ,则可以使用:

If you want to match longer expressions like 2+3+4+5=14 then you can use:

^\d+\s*([+-]\s*\d+\s*)+=\s*\d+$

说明:

^\d+                   # first operand
\s*                    # 0 or more spaces
(                      # start repeating group
 [+-]\s*               # the operator (+/-) followed by 0 or more spaces
 \d+\s*                # 2nd (3rd,4th) operand followed by 0 or more spaces
)+                     # end repeating group. Repeat 1 or more times.
=\s*\d+$               # equal sign, followed by 0 or more spaces and result.

现在,您可能想要接受 2 = 2这样的表达式作为有效表达式。在这种情况下,可能没有重复的组,因此将 + 更改为 *

Now, you might want to accept an expression like 2=2 as a valid expression. In that case the repeating group could be absent, so change + into *:

^\d+\s*([+-]\s*\d+\s*)*=\s*\d+$

查看示例2

这篇关于正则表达式以匹配数字和运算符之间的空格,但数字之间没有空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆