[LON-CAPA-users] Regular expressions in string response
Gerd Kortemeyer
lon-capa-users@mail.lon-capa.org
Tue, 1 May 2007 14:31:24 -0400
Hi,
On May 1, 2007, at 2:17 PM, Robert_Brewington@er.monroe.edu wrote:
> I want the answer
> to recognize a chemical equation; the problem is, the kids put
> extra or no
> blanks randomly between the compound formulas. For example, the answer
>
> 2Ag + Ca(NO3)2 -> 2AgNO3 + Ca
>
> should also recognize
>
> 2Ag + Ca(NO3)2 -> 2AgNO3 + Ca
> 2Ag+Ca(NO3)2->2AgNO3+Ca
> Ca(NO3)2 + 2Ag -> 2AgNO3 + Ca
> 2Ag + Ca(NO3)2 -> Ca + 2AgNO3
>
> and so on.
Sounds like a job for chemical reaction response ... did that somehow
not work?
If there was a problem, it seems we should fix that response type.
>
> For the moment, I am just trying to get it to accept the first
> answer. I
> can get the pipe part to work once this is right. I have my regular
> expression as
>
> $regexpans = '/^(2Ag\s*\+\s*Ca(NO3)2\s*->\s*2AgNO3\s*\+\s*Ca)\s*$/';
>
> where I backslashed the "+" because the regular expression routine
> thought
> it was part of the \s* construct.
Correct.
> This expression is not working, though.
> With or without spaces, no answer is accepted. I assume I am doing
> something simple wrong:(
I think you need to escape the brackets around "NO3" in Ca(NO3), i.e.,
\(NO3\)
Also, I am not sure about the "-" and the ">", which is something
else I routinely escape, just to be on the safe side.
But again, chemresponse should handle this much more elegantly and
right out of the box.
- Gerd.