Java 用于匹配数字和运算符之间的空格但数字之间不匹配空格的正则表达式
我在这个论坛上搜索了很多帖子,令我惊讶的是,我还没有找到像我这样有问题的人。 我必须从控制台为字符串值制作一个简单的计算器。现在,我正在尝试创建一些正则表达式来验证输入。 我的计算器必须接受运算符之间有空格的数字(只允许+和-),但不能接受数字之间有空格的数字,总之: 2+2=4是正确的,但是 2+2-->这将产生一个错误,并通知控制台上的用户在数字之间留有空格 我想到了这个:Java 用于匹配数字和运算符之间的空格但数字之间不匹配空格的正则表达式,java,regex,string,calculator,Java,Regex,String,Calculator,我在这个论坛上搜索了很多帖子,令我惊讶的是,我还没有找到像我这样有问题的人。 我必须从控制台为字符串值制作一个简单的计算器。现在,我正在尝试创建一些正则表达式来验证输入。 我的计算器必须接受运算符之间有空格的数字(只允许+和-),但不能接受数字之间有空格的数字,总之: 2+2=4是正确的,但是 2+2-->这将产生一个错误,并通知控制台上的用户在数字之间留有空格 我想到了这个: static String properExpression = "([0-9]+[+-]?)*[0-9]+$"; s
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");
你能给我一个关于我应该使用的正则表达式的提示吗?来匹配一个完整的表达式,比如
2+3=24
或者6-4=2
,一个类似正则表达式的正则表达式
^\d+\s*[+-]\s*\d+\s*=\s*\d+$
行。看看你可以在哪里玩
如果要匹配较长的表达式,如2+3+4+5=14
,则可以使用:
^\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
这样的表达式作为有效表达式。在这种情况下,重复组可能不存在,因此将+
更改为*
:
^\d+\s*([+-]\s*\d+\s*)*=\s*\d+$
看看那个。来匹配一个完整的表达式,比如
2+3=24
或者6-4=2
,一个类似正则表达式的正则表达式
^\d+\s*[+-]\s*\d+\s*=\s*\d+$
行。看看你可以在哪里玩
如果要匹配较长的表达式,如2+3+4+5=14
,则可以使用:
^\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
这样的表达式作为有效表达式。在这种情况下,重复组可能不存在,因此将+
更改为*
:
^\d+\s*([+-]\s*\d+\s*)*=\s*\d+$
看看有没有那个。试试:
^(?:\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.
和^
锚定正则表达式以匹配整个字符串$
- 我添加了
,以允许每个数字/运算符之间存在空格\s*
- 我用
替换了\d
,只是为了稍微简化一下;两者是等价的[0-9]
=
,因为您的问题提到了这一点,但您尝试的properExpression
表达式没有尝试。如果是这种情况,应该很容易看到如何修改表达式以支持它。尝试:
^(?:\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.
和^
锚定正则表达式以匹配整个字符串$
- 我添加了
,以允许每个数字/运算符之间存在空格\s*
- 我用
替换了\d
,只是为了稍微简化一下;两者是等价的[0-9]
我有点不清楚您是否希望允许/不允许在结尾处包含
=
,因为您的问题提到了这一点,但您尝试的properExpression
表达式没有尝试。如果是这种情况,应该很容易看到如何修改表达式以支持它。请注意,我没有试图解决除正则表达式问题以外的任何其他问题。
尽可能地保持你的逻辑流程。虽然还有其他更有效的答案,但你必须改变你的逻辑流程很多
请看下面的内容,如果您有任何问题,请告诉我
static String properExpression = "\\s*(\\d+\\s*[+-]\\s*)*\\d+\\s*";
static String noInput = "";
static String numbersWithSpaces = ".*\\d[\\s]+\\d.*";
//I've tried also "[\\d\\s+\\d]";
static void validateUserInput() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a calculation.");
String input = sc.nextLine();
if(input.matches(properExpression)) {
input=input.replaceAll(" ", ""); //You've to assign it back to input.
calculator.calculate(); //Hope you have a way to pass input to calculator object
} else if(input.matches(noInput)) {
System.out.print(0);
} 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");
工作版本样本
解释 下面允许使用可替换空间
\\s* //Allow any optional leading spaces if any
( //Start number+operator sequence
\\d+ //Number
\\s* //Optional space
[+-] //Operator
\\s* //Optional space after operator
)* //End number+operator sequence(repeated)
\\d+ //Last number in expression
\\s* //Allow any optional space.
带空格的数字
.* //Any beginning expression
\\d //Digit
[\\s]+ //Followed by one or more spaces
\\d //Followed by another digit
.* //Rest of the expression
请注意,除了正则表达式问题之外,我没有试图解决任何潜在的问题。 尽可能地保持你的逻辑流程。虽然还有其他更有效的答案,但你必须改变你的逻辑流程很多 请看下面的内容,如果您有任何问题,请告诉我
static String properExpression = "\\s*(\\d+\\s*[+-]\\s*)*\\d+\\s*";
static String noInput = "";
static String numbersWithSpaces = ".*\\d[\\s]+\\d.*";
//I've tried also "[\\d\\s+\\d]";
static void validateUserInput() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a calculation.");
String input = sc.nextLine();
if(input.matches(properExpression)) {
input=input.replaceAll(" ", ""); //You've to assign it back to input.
calculator.calculate(); //Hope you have a way to pass input to calculator object
} else if(input.matches(noInput)) {
System.out.print(0);
} 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");
工作版本样本
解释 下面允许使用可替换空间
\\s* //Allow any optional leading spaces if any
( //Start number+operator sequence
\\d+ //Number
\\s* //Optional space
[+-] //Operator
\\s* //Optional space after operator
)* //End number+operator sequence(repeated)
\\d+ //Last number in expression
\\s* //Allow any optional space.
带空格的数字
.* //Any beginning expression
\\d //Digit
[\\s]+ //Followed by one or more spaces
\\d //Followed by another digit
.* //Rest of the expression
您没有找到答案的原因可能是因为正则表达式不是确定字符串是否为有效数学公式的好工具!当你开始添加其他操作符,如括号、指数等,会发生什么情况?…但是如果我们把问题限制在你提到的简单类型的方程上,那么当然可以用正则表达式来完成。使用下面的正则表达式\d*\s\+\s\d*\s虽然我同意@TomLord,但我仍然会在使用正则表达式时尝试帮助你解决问题。为什么不使用
\d+\s+\d+
来检测一个数与数之间有空格的方程呢?这只是我必须做的一些糟糕的家庭作业,所有其他操作都应该被视为错误,所以这个计算器永远不会有更多的选项。放括号或指数应该会出错,因为它不在其规格中。我想所有这些限制实际上只会让事情变得更糟:)你没有找到答案的原因可能是因为正则表达式不是确定字符串是否是有效数学方程的好工具!当你开始添加其他操作符,如括号、指数等,会发生什么情况?…但是如果我们把问题限制在你提到的简单类型的方程上,那么当然可以用正则表达式来完成。使用下面的正则表达式\d*\s\+\s\d*\s虽然我同意@TomLord,但我仍然会在使用正则表达式时尝试帮助你解决问题。为什么不使用\d+\s+\d+
来检测一个数与数之间有空格的方程呢?这只是我必须做的一些糟糕的家庭作业,所有其他操作都应该被视为错误,所以这个计算器永远不会有更多的选项。放括号或指数应该会出错,因为它不在其规格中。我想所有这些限制真的只会让事情变得更糟:)谢谢!是的,当然我有一个计算器课,可以计算方程式。带空格的数字的正则表达式也很有效,我想我得花点时间单独处理正则表达式