Reverse Polish notation in Java. Checking for any symbols or characters (regex), that should not be in a string array

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

Reverse Polish notation in Java. Checking for any symbols or characters (regex), that should not be in a string array

问题

我正在尝试弄清楚为什么这段代码不起作用因为表示法 "156 154 152 - 3 + -" 应该可以正常运行而不抛出异常在这种情况下也许有更好的方法来使用正则表达式吗当我实际运行我的解释函数而不手动抛出异常时结果是正确的一切都很好但是对于这个练习有一个对这种异常处理的要求
下面是代码

public class RegexTest {
    public static void main(String[] arg) {

        boolean b = check_notation("156 154 152 - 3 + -");
        System.out.println(b);
    }

    public static boolean check_notation(String pol){

        pol = pol.trim();
        String[] tokens = pol.split("\\s+");

        for (String r : tokens) {
            if (!(r.matches("-[0-9]*") || r.matches("[0-9]*") || r.equals("/") || r.equals("*") || r.equals("-") || r.equals("+"))) {
                throw new RuntimeException("存在不允许的符号。");
            }
        }

        return true;
    }
}
英文:

I'm trying to figure out why this piece of code is not working, as the notation "156 154 152 - 3 + -" should go through without throwing an exception. Is there maybe a better way to use regex in this case? When I actually run my interpret function without manually throeing the exception, the result is correct and all is good. But for this exercise, there is a requirement for such exception handling.
Here's the code:


public class RegexTest {
    public static void main(String[] arg) {

        boolean b = check_notation("156 154 152 - 3 + -");
        System.out.println(b);
    }

    public static boolean check_notation(String pol){

        pol = pol.trim();
        String[] tokens = pol.split("\\s+");

        for (String r : tokens) {
            if (!(r.matches("-[0-9]*") || r.matches("[0-9]*") || r == "/" || r == "*" || r == "-" || r == "+")) {
                throw new RuntimeException("There are symbols that are not allowed.");
            }
        }

        return true;
    }
}

答案1

得分: 1

@searchfind ... r.matches()对于特殊字符正则表达式的检查是必需的。另外,在 + 和 * 符号正则表达式之前加上 \\,以避免悬挂的元字符异常。<br>
请使用以下正则表达式条件:

if (!(r.matches(&quot;-[0-9]*&quot;) || r.matches(&quot;[0-9]*&quot;) || r.matches(&quot;/&quot;) || 
            		r.matches(&quot;\\*&quot;) || r.matches(&quot;-&quot;) || r.matches(&quot;\\+&quot;))) {
                throw new RuntimeException(&quot;不允许出现特殊符号&quot;);
            }
英文:

@searchfind ... r.matches() was required for the special characters regex check. Also; \\ needs to be prefixed before + and * symbol regex to avoid dangling meta character exceptions.<br>
Kindly use below Regex conditions

if (!(r.matches(&quot;-[0-9]*&quot;) || r.matches(&quot;[0-9]*&quot;) || r.matches(&quot;/&quot;) || 
            		r.matches(&quot;\\*&quot;) || r.matches(&quot;-&quot;) || r.matches(&quot;\\+&quot;))) {
                throw new RuntimeException(&quot;There are symbols that are not allowed.&quot;);
            }

huangapple
  • 本文由 发表于 2020年10月11日 23:53:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/64306049.html
匿名

发表评论

匿名网友

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

确定