validate mathematical variables expression in java -
which way validate mathematical variables expression. whether can use stack or regex expression.
i have gone http://www.sanfoundry.com/java-program-implement-evaluate-expression-using-stacks/
note: expression not contain parenthesis. example:
valid:
x+3-y/2
x+y
2+4+e
invalid:
x++4+8
e-3r
e+r34-
how validate above expressions?i need validate mathematic expression input provided user in java
if no parentheses (i.e. nesting expressions) allowed, validation done simple regex:
final pattern pattern = pattern.compile("([0-9]+|[a-z]+)([-+*/]([0-9]+|[a-zaz]+))*"); (string s : arrays.aslist("123", "x+3-y/2", "x+y", "2+4+e", "x++4+8", "e-3r", "e+r34-", "---x+3-y/2")) { system.out.format("%5s %s\n", pattern.matcher(s).matches(), s); }
the output is
true 123 true x+3-y/2 true x+y true 2+4+e false x++4+8 false e-3r false e+r34- false ---x+3-y/2
Sir can you upload computer science mcqs with answers ppsc on your site it will be nice of you..
ReplyDelete