简体   繁体   English

如何在 Java 中使用正则表达式匹配有效的操作序列

[英]How can I match valid sequence of operation using Regular Expression in Java

I just started to learn about Regular Expression with Java.我刚开始用 Java 学习正则表达式。 I have this project from hyperskill.com called "Smart Calculator" it is like the Python interpreter where you can input a arithmetic operation and when you press enter there is an output.我有这个来自hyperskill.com的项目,称为“智能计算器”,它就像 Python 解释器,您可以在其中输入算术运算,当您按下回车键时,会出现 Z78E6221F6393D1356681DB398Z1。

I would like to match if the input is valid like 2 + 2 - 3 and won't match if the input is 2 + 2 - .如果输入像2 + 2 - 3一样有效,我想匹配,如果输入是2 + 2 - + 2 - 则不匹配。

I have these regular expression (\s*[0-9]+\s*[\+\-\*\/]+\s*[0-9]+\s*){1,}我有这些正则表达式(\s*[0-9]+\s*[\+\-\*\/]+\s*[0-9]+\s*){1,}

But when I tried to test with some inputs...但是当我尝试使用一些输入进行测试时......

[In]    1 + 1
[Out]   Matched

[In]    1 + 1 + 1    // Not matched
[Out]   Not Matched

[In]    1 + 12 + 1  //But this one matched
[Out]   Matched

[az] is regexpese for: Any character between a and z. [az]是正则表达式:a 和 z 之间的任何字符。 So, your [+-*/] is regexpese for: "Any character having a unicode codepoint value between the codepoint value of + and the codepoint value of * which is problematic. Try putting the - at the start or end, in which case it does mean literally the - character, or escape it. So, make that: [-+*/] .因此,您的[+-*/]是正则表达式:“任何在+的代码点值和*的代码点值之间具有 unicode 代码点值的字符都有问题。尝试将-放在开头或结尾,在这种情况下它的字面意思是-字符,或者转义它。所以,这样写: [-+*/]

Secondly, your regexp is for:其次,您的正则表达式适用于:

  • A number一个号码
  • an operator操作员
  • A number一个号码

which you then state as: This, at least once or more (note that {1,} is just a silly way of writing + !然后您将 state 视为:这,至少一次或多次(注意{1,}只是一种愚蠢的写作方式+

thus, you've set up the regexp to look for example for:因此,您已经设置了正则表达式以查找例如:

  • A number一个号码
  • an operator操作员
  • A number一个号码
  • A number一个号码
  • an operator操作员
  • A number一个号码

That duplication of 'a number' in the middle is your problem.中间的“数字”重复是你的问题。 The reason 1+12+1 matches is because your regexp matches that as 1+1 followed by 2+1 . 1+12+1匹配的原因是因为您的正则表达式将其匹配为1+1后跟2+1 That's also the reason why 1+1+1 does not match.这也是1+1+1不匹配的原因。

The solution is to start with a number, and then repeat only the 'operator, then number' part.解决方案是以数字开头,然后只重复“运算符,然后是数字”部分。

Note that \d is short for digit, shorter than [0-9].注意\d是数字的缩写,比 [0-9] 短。

Finally, you've said that any number of operators is okay, which makes no sense.最后,您说过任何数量的运算符都可以,这没有任何意义。 try it: 1++1 is valid according to your regex here.试试看:根据您的正则表达式, 1++1是有效的。

Putting it all together:把它们放在一起:

\s*\d+(\s*[-+*/]\s*\d+)+

is all you need.是你所需要的全部。

Use the regex, (\s*[0-9]+\s*([+-]+\s*[0-9]+\s*)*) .使用正则表达式(\s*[0-9]+\s*([+-]+\s*[0-9]+\s*)*) Check this for an explanation of the regex.检查这个以获得正则表达式的解释。

public class Main {
    public static void main(String[] args) {
        String[] arr = { "1 + 1", "1 + 1 + 1", "2 + 2 - ", "2 + 2 - 3", "1 + 12 + 1" };
        for (String s : arr) {
            System.out.println(s + " => " + s.matches("(\\s*[0-9]+\\s*([+-]+\\s*[0-9]+\\s*)*)"));
        }
    }
}

Output: Output:

1 + 1 => true
1 + 1 + 1 => true
2 + 2 -  => false
2 + 2 - 3 => true
1 + 12 + 1 => true

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

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