Jump to content

Regular expression help


mrithula

Recommended Posts

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 />';
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.