I have an arithmetic expression
((20+30)-25)/5
I want to validate by using regular expression. The expression can only have integers, floating point numbers, operands and parenthesis.
How can I generate regular expression to validate please help or suggest any other way to validate that string using javascript.
As I said in a comment, this is impossible using one JavaScript regular expression. However, you can do it using a loop: replace subexpressions with atoms, repeat until you get an atom. If you can't reduce any more, and whatever is left is not an atom, it does not validate. This is actually pretty much the same procedure you'd do to evaluate it (just skipping the abstract syntax tree). You can search for \(\d+\)|\d+[-+/*]\d+ and replace with 0:
Example:
((20+30)-25)/5
((0)-25)/5
(0-25)/5
(0)/5
0/5
0
Done
If you failed to match and didn't have just 0, it's a fail.
(To evaluate as opposed to validate, you'd just have to be replacing with with the actual value rather than a dummy stand-in, everything else is the same).
JavaScript "eval" function is the best validator.
Try to do this:
eval("((20+30)-25)5");
and you will get sufficiently detailed error description.
You will only be able to do this with regular expressions if you impose a maximum depth to the parenthesis nesting. Otherwise, the set of arithmetic expressions forms a context free language but not a regular language.
If I had to use regex, the approach I would use is to write a regular grammar for your set of arithmetic expressions and then convert that to a regular expression.
Another approach is to write a recursive descent parser, which is a fairly simple project and works very nicely for arithmetic expressions.
Related
Looking for pre-processor for creating own syntax of regular expression, based on RegExp & PCRE syntax so it can be parsed to PCRE syntax. Example at the end
I guess I need a processor of regular expression that outputs a tree structure that represents regular expression, so I can traverse the tree and hotswap some parts, then compile it to regular expression string.
But this processor must have ability to add own syntax parsing/processing.
Is there some processor like this, already made by someone? I've made one by myself some time ago, but looking for more professional solution.
Of course we are talking about node.js/javascript
Yes, node.js has not support for PCRE, but there is a npm module for using PCRE with node.js, it works great!
Why someone would need it?
For example, you can create big regular expression by smaller ones:
(John (like|love)s every (animal|creature) on earth: (#animals))
(#...) is hash tag group, it means in place of it will be another regular expression containing alterantives for all animals.
Another example, you can create more sophisticated kind of groups:
(#(a|x)(b)(c))
permutation group matches all brackets (3 or less or more) in any order:
(a|x)(b)(c)
(a|x)(c)(b)
(b)(a|x)(c)
(b)(c)(a|x)
(c)(a|x)(b)
(c)(b)(a|x)
have more, but I guess I've made a point.
Earlier questions on StackOverflow discuss escaping for JavaScript regular expressions, e.g.:
How to escape regular expression in javascript?
Escape string for use in Javascript regex
An implementation suggested is the following:
RegExp.quote = function(str) {
return str.replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
};
Given that regular expressions in the two languages are not identical, is anyone aware of a JavaScript method that properly escapes strings to be used for Java regular expressions?
There's no need for any escaping at all. Those questions are about what needs to be done when the regular expression is being constructed as a string in the source language. Since you're reading the string from an input field, there's no layer of interpretation to worry about.
Just send the string to the server, where it will be discovered to be a valid regex or not.
edit — though I can't think of any, the real thing to worry about might be any sort of "injection" attack that could be conducted through this avenue. Seems to me that if you're just passing a regex to Pattern.compile() there aren't any side-effect channels that could be exploited.
Hey I've written a fractal-generating program in JavaScript and HTML5 (here's the link), which was about a 2 year process including all the research I did on Complex math and fractal equations, and I was looking to update the interface, since it is quite intimidating for people to look at. While looking through the code I noticed that some of my old techniques for going about doing things were very inefficient, such as my Complex.parseFunction.
I'm looking for a way to use RegExp to parse components of the expression such as functions, operators, and variables, as well as implementing the proper order of operations for the expression. An example below might demonstrate what I mean:
//the first example parses an expression with two variables and outputs to string
console.log(Complex.parseFunction("i*-sinh(C-Z^2)", ["Z","C"], false))
"Complex.I.mult(Complex.neg(Complex.sinh(C.sub(Z.cPow(new Complex(2,0,2,0))))))"
//the second example parses the same expression but outputs to function
console.log(Complex.parseFunction("i*-sinh(C-Z^2)", ["Z","C"], true))
function(Z,C){
return Complex.I.mult(Complex.neg(Complex.sinh(C.sub(Z.cPow(new Complex(2,0,2,0))))));
}
I know how to handle RegExp using String.prototype.replace and all that, all I need is the RegExp itself. Please note that it should be able to tell the difference between the subtraction operator (e.g. "C-Z^2") and the negative function (e.g. "i*-(Z^2+C)") by noting whether it is directly after a variable or an operator respectively.
While you can use regular expressions as part of an expression parser, for example to break out tokens, regular expressions do not have the computational power to parse properly nested mathematical expressions. That is essentially one of the core results of computing theory (finite state automata vs. push down automata). You probably want to look at something like recursive-descent or LR parsing.
I also wouldn't worry too much about the efficiency of parsing an expression provided you only do it once. Given all of the other math you are doing, I doubt it is material.
Just out of curiosity, is it possible to parse a string that is totally made out of random but valid regular expressions with a single regular expression?
given the string of regex:
<[^>]*>\xA9
parses to:
<[^>]*>
\xA9
in which the first one match html and second one match a copyright symbol.
Edit:
I found a similar question asked at SO claiming that it maybe possible. Here, I'm referring to regex in JavaScript ECMA-262 only.
No, it is not possible: regular expression language allows parenthesized expressions representing capturing and non-capturing groups, lookarounds, etc., where parentheses must be balanced. It is not possible even in theory to write a regular expression that verifies if parentheses are balanced in a given string. Without an ability to do that you wouldn't know where one regexp ends and the other one starts.
In general, regex grammar is relatively complex. To get an idea of just how complex it is, take a look at the parser in the source of Java's Pattern class.
I am trying to create a regular expression that checks for letters, numbers, and underscores. In .NET, I can do "^\w+$". However, I am not that familiar with the JavaScript syntax. Can somebody help me out?
Thank you!
One obvious difference is that in JavaScript, you write the regex as /pattern/flags -- this is Perl-style. Your "example" would then be ^\w+$ → /^\w+$/.
For example, replace multiple e's with one e, case-insensitive search (hence the i flag):
var s='qweEEerty';
s=s.replace(/e+/i, 'e');
Returns: qwerty.
That same expression will work in JavaScript (there are some differences between .NET regular expressions and JavaScript regular expressions but not in this example).
I recommend that you read Using Regular Expressions with JavaScript and ActionScript to learn a bit more about JavaScript's regular expression implementation.