[LON-CAPA-users] Regular expressions in string response
lon-capa-users@mail.lon-capa.org
lon-capa-users@mail.lon-capa.org
Tue, 1 May 2007 14:17:02 -0400
Yeah, I used to use a lot of regular expressions back in my software
development days (in vi). Very powerful, very cool, very hard to maintain.
So, I tried to get this to work, still not quite right. 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.
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. This expression is not working, though.
With or without spaces, no answer is accepted. I assume I am doing
something simple wrong:(
The correct string (as above) shows in the answer field when I test as
author. Oh, yes I did remember to specify regular expressions in the
stringresponse, and it works with other regular expressions.
brew
-----lon-capa-users-admin@mail.lon-capa.org wrote: -----
To: lon-capa-users@mail.lon-capa.org
From: Gerd Kortemeyer <korte@lite.msu.edu>
Sent by: lon-capa-users-admin@mail.lon-capa.org
Date: 05/01/2007 09:14AM
Subject: Re: [LON-CAPA-users] Regular expressions in string response
Hi,
On May 1, 2007, at 8:33 AM, Robert_Brewington@er.monroe.edu wrote:
>
> I am trying to set up a string response question that takes the
> student
> answer and removes blanks before comparing to the correct answer.
> Hopefully
> someone has a code snippet that does this?
>
> Student input:
> a b c
>
> I want to match to
> /^(abc|acb|bca)\b)
\s* matches any number of spaces, including zero:
/^(a\s*b\s*c|a\s*c\s* ...) ...
>
> (Incidentally, what does the "b" do in this expression?)
Matches a word boundary, and is not appropriate if you want to
consider spaces. Your whole expression would be
/^\s*(a\s*b\s*c|a\s*c\s*b|b\s*c\s*a)\s*$/
>
> I am looking for something like the vi command to remove spaces:
> s/ //g
Rather than removing them, I would match them.
See
http://www.regular-expressions.info/
As the saying goes:
You have a problem, and think: "Oh, I'll just use a regular
expression!" - now you have two problems.
But seriously, I love regular expressions.
- Gerd.
_______________________________________________
LON-CAPA-users mailing list
LON-CAPA-users@mail.lon-capa.org
http://mail.lon-capa.org/mailman/listinfo/lon-capa-users