in reply to '+' to +
I also agree with monarch that as you know what the operator can be you can use a character class.#!/usr/bin/perl use strict; use warnings; my $problem = '1 / 2 = '; my ($number1, $number2, $operator); if ($problem =~ /(\d+)\s*([-+*\/])\s*(\d+)/){ ($number1, $number2, $operator) = ($1, $2, $3); print "$number1 $number2 $operator\n"; } else{ print "regex failed\n"; }
FWIW. I always have to check the docs to check which characters need escaping. They say that:
The special characters for a character class are -]\^$ and are matched using an escape...Now I'm sure that's a backslash in there. But I couldn't get the regex above to work without escaping the forward slash.
Anyone see what I'm missing here?
Update:
It's because I used the forward slash as the quote for the regex!
Many thanks to the monks in the CB.
|
---|