in reply to how would you detect a math expression
This will not work for your example because it does not recognise floating point numbers.$text = 'speed of light in km/fortnight'; if ($text =~ /(.*)\s+in\s+(.*)/) { print "1: ($1) ($2)\n"; } $text = '2+8/2*3'; while (length $text) { if ($text =~ m|(\d+)\s*([/*])\s*(\d+)|) { print "($1) $2 ($3)\n"; $x = eval " $1 $2 $3 "; $text =~ s|\d+\s*[/*]\s*\d+|$x|; } else { last; } } while (length $text) { if ($text =~ m|(\d+)\s*([+-])\s*(\d+)|) { print "($1) $2 ($3)\n"; $x = eval " $1 $2 $3 "; $text =~ s|\d+\s*[+-]\s*\d+|$x|; } else { last; } } print "answer $text\n";
|
|---|