Hello All, This is the first time I am posting question in forum , if any mistakes found please forgive me. My question is, Regarding How to write a Regular Expression in javascript for my problem.. if i give expressions as c1+c24 c1+c12+c3 (c1+c12) (c10+c2+c33) (c1+c2)+(c1+c4) c1&&c2 c3||c2&&c6 c2<=c4 c1>=c12 c4>c23 c4<c24 c4==c3==c1 have to return " true " value otherwise if i give expressions as , c1++c2 c1++c2+c3 c1+c2++c3 c1++c2++c3 (c1+c2)++ c1++ (c1++c2) (c1+c2)+(c14++c6) (c1++c2)++(c5++c7) c1&&&&c2 c1||||||c3&&&&c6 c2<=<=<=c4 c1>=>=c12 c4>>>c23 c4<<c24 c4=====c3=====c1 (c1+c2)<=<=(c14++c6) (c1++c2)&&&&(c5++c7) hav to return " false " value What I mean to say is if I take any operator (i.e., +,-,/*.&&,||,<,<=,>,>=,==) more than one same operator like ++,+++ etc., with or without paranthesis ,that regular expression have to retrun " false " value. Otherwise it have to return " true " value. Now i am using the below regular expression,but for some expressions only this is working, function isValidExpr(expr) { return /^([-+]{2})?(([+-]?c\d+)|([+-]?c\d+)([-+*\/]([+-]?c\d+))+|\(([+-]?c\d+)([-+*\/]([+-]?c\d+))+\))([-+]{2})?([-+*\/]([-+]{2})?(([+-]?c\d+)|([+-]?c\d+)([-+*\/]([+-]?c\d+))+|\(([+-]?c\d+)([-+*\/]([+-]?c\d+))+\))([-+]{2})?)+$/i.test(expr); } But not for all expressions... Please solve my problem as soon as possible,in my office i asked so many people but no one worked on this regular expression.. I am expecting you all people can answer my question... Waiting for all your replies... Regards, Ayeesha Shaik
Give us some more info, what are the specific problems with matching? Noone is going to voluntarily crunch through that mess. Anyway, I think you've taken the wrong approach. I would separate into terms first, e.g. using a preg_split and then deal with it. That mammoth regex is a joke.
Krt, I have text area and button in web page, with button onclick event i have to validate different expressions with different operators and return that correct expression to another page's text area. For example, first type validation:: =============== If I take c1+c2 , c1+c2+c3 , c1&&c2 , c1<=200 , (c1+c2)+(c4+c7), (c1+c2) , (c1+c7)+(c3) etc., then it has to return true value After button click ,this correct expression have to return (like c1+c2 )to other page's text area. Second type validation:: ================= If I take expressions as ,(I mean more than one with same operator,like +++ dont have to allow ,have to raise alert dialogue box) c1&&&&c2 c1||||||c3+c4 c1--c2 c1****c4 etc., (c1++c3) (c4++c6)+(c2) (c12+c3)++(c2+c4) (c12++c3)&&&&&(c2++c4) (c13+c5)+(c3||||c7) have to return false value and raise alert dialogue box with message " Invalid Expression" these validations have to do.. All my expressions in the form of c's only like(c1,c2,c3 etc.,) Can you please suggest me how to write the relted regular expression. Regards, Ayeesha Shaik
Hello Ayeesha, what you want can't be done with regular expression, i think... You cannot test something like this with regex: [a]{n} {n} where n = 1,2,... What i specific mean you cannot test following with regex aaabbb or aaaababbbb or aaaabbbb, etc... The main problem are the brackets, you cannot make sure with regular expressions alone that a term contains the same number of opening and closing brackets. Someone correct me if i'm wrong. I would suggest you take another approach and make your own custom parser which will run through the given expression, only then you can make sure it's correct. Regards