regular expressions question

regular expressions question

Jeroen Hollander -
回帖数:9

Hello,

I'm using the gap fill question type (fill in the blanc) to have students give the Spanish translation for some words.

I want to use regular expressions to check 2 things:
a. the article (el / la) is optional, but when entered it must be correct.
b. student can type the word with or without accents.

The first question works okay: the horse --> el cabello ==> (el )?caballo
I can type the answer with or without the article, and using "la" marks the answer as wrong.

The next question doesn't work okay: the bird --> el pájaro ==> (el )?p[á|a]jaro
The answers "pajaro" and "pájaro" are marked correct. That is okay.
Also "el pajaro" and "el pájaro" are marked correct. That is okay too.
But repacing "el" with ANY other wordt marks to answer as correct too. That is not okay.

So I assume the problem is in using both ()? and [|] expression in one question. 
How can I fix this?

Thank you in advance.

Best regards,

Jeroen




回复Jeroen Hollander

Re: regular expressions question

Joseph Rézeau -
Core developers的头像 Particularly helpful Moodlers的头像 Plugin developers的头像 Testers的头像 Translators的头像

Hi Jeroen,

Here's the solution:

1.- In your gapfill question go to More Options -> Delimit characters and select a delimiter that is different from the default [ ], e.g. { }

2.- The following coding works:

the horse --> el cabello ==> {(el )?caballo}

the bird --> el pájaro ==> {(el )?p[á|a]jaro}

That is explained in the excellent眨眼 documentation here: https://docs.moodle.org/38/en/Gapfill_question_type#Regular_Expressions

回复Joseph Rézeau

Re: regular expressions question

Jeroen Hollander -

Thank you for your reply.

I was already using different delimiters 微笑

#(el )?caballo#
#(el )?p[á|a]jaro#

The main problem is that "el" can be replaced with any other word in the second question (although the syntax is quite similair like in the first question).

Hope you can help me.


Jeroen

回复Jeroen Hollander

Re: regular expressions question

Joseph Rézeau -
Core developers的头像 Particularly helpful Moodlers的头像 Plugin developers的头像 Testers的头像 Translators的头像
Sorry, Jeroen,
I had not tested my solution well enough. I will have another look as soon as possible.
回复Joseph Rézeau

Re: regular expressions question

Joseph Rézeau -
Core developers的头像 Particularly helpful Moodlers的头像 Plugin developers的头像 Testers的头像 Translators的头像

There is definitely something wrong with that regular expression analysis. I seems to be the space in the first optional element that causes the problem.

I leave it to the plugin author, Marcus, to fix this bug.

回复Joseph Rézeau

Re: regular expressions question

Marcus Green -
Core developers的头像 Particularly helpful Moodlers的头像 Plugin developers的头像 Testers的头像
It is a bug caused by a repurposing of the | operator.
The clue comes from this line of code
$answerparts = explode("|", $answer);
There is a bit of a work around like this
#(el )?p[áa]jaro#
But it means the | operator cannot be used as you would expect in regex 伤心
Not sure I can fix this without breaking a large number of existing questions.
回复Marcus Green

Re: regular expressions question

Jeroen Hollander -

Hello Marcus,


Thank you for looking into this.

Your suggested solution is a okay for the moment.


Thanks again.


Jeroen

回复Jeroen Hollander

Re: regular expressions question

Marcus Green -
Core developers的头像 Particularly helpful Moodlers的头像 Plugin developers的头像 Testers的头像
My apologies that this issue has taken up your time. Not many people use the regex capability so it doesn't get tested as much as it should. I will add a unit test that docments the effect of the way I repurpose the | operator and also add a note to the documentation of this issue.
In the documentation I mention two other question types that use regular expressions you might be interested in, see
https://docs.moodle.org/38/en/Gapfill_question_type#Regular_Expressions
One of which is maintained by Joseph
回复Marcus Green

Re: regular expressions question

Jeroen Hollander -
Hallo Marcus,

I'm back again with another "problem".
We'd like our students to translate a sentence into Spanish.
The current answer is: #Veis [el|la] m[aá]s grande#
But it doesn't work. Both "mas" and "más" are accepted, but I can't get a check on "el" or "la" to work.
Do you have any suggestions?