mrithula Posted September 1, 2010 Share Posted September 1, 2010 I need regular expression to validate the following input possible inputs: sin(x)+cos(x) cos(x)-sin(x) sin(x)-cos(x) Error shoud be displayed if any other character except sin(x),cos(x),+ and - is used can some one help me? Quote Link to comment https://forums.phpfreaks.com/topic/212259-regular-expression-help/ Share on other sites More sharing options...
wildteen88 Posted September 1, 2010 Share Posted September 1, 2010 Basic regex pattern ~(cos|sin)\([a-z0-9\.]+\)[+-](cos|sin)\([a-z0-9\.]+\)~ Test code $strings = array( 'sin(x)+cos(x)', 'cos(x)-sin(x)', 'sin(x)-cos(x)', 'sin(x)*cos(x)', 'sin(x)/cos(x)', 'sin(x)+sin(x)', 'cos(x)+cos(x)', ); foreach($strings as $string) { preg_match('~(cos|sin)\([a-z0-9\.]+\)[+-](cos|sin)\([a-z0-9\.]+\)~', $string , $matches); echo $string . ' - ' . ( isset($matches[0]) && ($matches[1] != $matches[2]) ? 'PASS' : 'FAIL' ) . '<br />'; } Quote Link to comment https://forums.phpfreaks.com/topic/212259-regular-expression-help/#findComment-1106026 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.