re problem
Brian Quinlan
brian at sweetapp.com
Fri Jan 25 14:11:35 EST 2002
> hi
> i am trying to create an re expression which will match y = mx + c,
> with m and c = a number upto 4 digits long. I first came up with
>
> self.linear = re.compile("y = [\d+?]x [+|-] \d+", re.IGNORECASE)
>
> this doesnt work though and i dont understand why.
> thanks
Well, first off you are using "[", which is for character sets, not
matching. Here is a start for you:
linear = re.compile("y \s* = \s* (\d*)x \s* ([+-]\s*\d+)", re.IGNORECASE
| re.VERBOSE)
>>> linear.match('y=2x+3').groups()
('2', '+3')
>>> linear.match('y=x-7').groups()
('', '-7')
>>> linear.match('y = 2322x - 7').groups()
('2322', '- 7') # notice internal space
I don't really understand what you are trying for:
1. can m be negative?
2. can there be a decimal point in m or c?
Cheers,
Brian
More information about the Python-list
mailing list