Match text pattern

Use a text pattern part when you want the student to enter short, non-mathematical text.

You can find the match text pattern part’s built-in marking algorithm at GitHub.

Marking

Match test

The test to use to decide if the student’s answer is correct.

  • The Regular expression test checks that the student’s answer matches the regular expression given in Answer pattern.

  • The Exact match test marks the student’s answer as correct only if it is exactly the same as the text given in Answer pattern. Space characters are removed from the start and end of the student’s answer as well as the answer pattern before comparison.

Answer pattern

The text or pattern the student must match.

When Match test is Regular expression, this is a regular expression defining the strings to be accepted as correct. If there are several valid answers, separate them with a | character. If you’re using the full regular expression functionality, note that ^ and $ are automatically added to the start and end of the answer pattern to ensure that the student’s whole answer matches the pattern.

You can substitute variables, the same as in content areas, by enclosing expressions in curly braces, e.g. {answervar}.

Display answer

A representative correct answer string to display to the student, in case they press the Reveal answers button. You can substitute variables by enclosing expressions in curly braces, the same as in content areas.

Must the answer be in the correct case?

If this is ticked, the capitalisation of the student’s answer must match that of the answer pattern. If it doesn’t, partial credit (defined using the slider below the checkbox) will be awarded.

Marking settings

The studentAnswer parameter is a string value, containing the string that the student typed in the answer box.

This part type provides the following properties to the settings object:

correctAnswer

The Answer pattern string.

displayAnswer

The Display answer string.

caseSensitive

Must the answer be in the correct case?

partialCredit

The proportion of credit to award if the answer is correct except for the case.

matchMode

The Match test setting: either "regex" or "exact".

Examples

A question in the Numbas demo demonstrates a variety of ways of using ‘match text pattern’ parts.

Numbas demo: match text pattern part type

In the following examples, the variable name has the value Epictetus

Regular expressions

Correct answer

Must be correct case?

Student answer

Correct?

Hello

Hello

Hello

hello

Hello

hello

Hello|Hi

Hi

(ab)+

ababab

[^d]+

abcefgh

[^d]+

abcdefgh

{name}

Epictetus

{name}( {name})+

Epictetus Epictetus

Exact match

Answer pattern

Must be correct case?

Student answer

Correct?

Hello

Hello

Hello

hello

Hello|Hi

Hi

Hello|Hi

Hello|Hi

{name}

Epictetus

{name}

epictetus

{name}

epictetus

{name} Jr.

Epictetus Jr.