ReferencematchesPattern

<matchesPattern>

Boolean condition that tests whether input matches a math pattern

<matchesPattern> is an Evaluation component that returns the boolean value of true or false depending on whether the enclosed math (often a referenced <mathInput/>) conforms to the specified pattern template.

The pattern attribute is required. Write the pattern the way you would write the expression itself, and list in parameters the variables that stand in for whatever the student writes there. Everything in the pattern that is not named in parameters has to appear literally.

Attributes and Properties

Attributes for <matchesPattern>

Highlighted (8)
allowImplicitIdentities

boolean. Default value: false. Whether to allow implicit identity transformations when matching.

allowPermutations

boolean. Default value: true. Whether to allow permutations of operands when matching.

excludeMatches

mathList. Patterns whose matches are excluded.

matchExpressionWithBlanks

boolean. Default value: false. Whether to allow expressions with blanks to match.

parameters

_variableNameList. Variables in the pattern that act as placeholders. Repeating one in the pattern requires the same subexpression at each occurrence. When specified, blanks in the pattern are matched literally.

pattern

math. Math expression pattern to match against.

requireNumericMatches

boolean. Default value: false. Whether numeric placeholders must match numbers.

requireVariableMatches

boolean. Default value: false. Whether variable placeholders must match variables.

Other (12)
allowedErrorInNumbers

number. Default value: 0. Maximum allowed numeric error when comparing numbers.

allowedErrorIsAbsolute

boolean. Default value: false. Whether allowedErrorInNumbers is interpreted as an absolute (rather than relative) tolerance.

caseInsensitiveMatch

boolean. Default value: false. Whether text comparisons ignore letter case.

expandOnCompare

boolean. Default value: false. Whether to expand math expressions before comparing.

includeErrorInNumberExponents

boolean. Default value: false. Whether the allowed numeric error also applies to numbers in exponents.

matchBlanks

boolean. Default value: false. Whether unfilled blanks in a math expression count as a match.

matchByExactPositions

boolean. Default value: false. Whether to match list values by exact position rather than by content.

numPeriodicSetMatchesRequired

integer. Default value: 3. Number of consecutive elements of a periodic set required to count as a match.

numSignErrorsMatched

number. Default value: 0. Maximum number of sign errors that still count as a match.

simplifyOnCompare

keyword. Level of simplification applied to math expressions before comparing.

ValueDescription
none (default)No simplification is applied before comparing.
fullFully simplify both expressions before comparing.
numbersSimplify numeric subexpressions only, leaving symbolic structure intact.
numbersPreserveOrderLike numbers, but does not reorder commutative operands.
normalizeOrderReorder commutative operands into a canonical form without simplifying values.
symbolicEquality

boolean. Default value: false. Whether children compared via this boolean use symbolic equality.

unorderedCompare

boolean. Default value: false. Whether order is ignored when comparing list-like values.

Common to all components (9)
copy

reference. Create an independent copy of another component by reference. Enter a references a $name.

disabled

boolean. Default value: false. Whether this component is disabled and cannot be interacted with.

extend

reference. Extend another component by reference, inheriting its children and attributes. Enter a reference as $name.

fixed

boolean. Default value: false. Whether this component's value is fixed and cannot be modified.

fixLocation

boolean. Default value: false. Whether this component's location is fixed (preventing it from being moved while still allowing other modifications).

hide

boolean. Default value: false. Whether to hide this component from the rendered output.

isResponse

boolean. Default value: false. Whether this component is treated as a response for the purposes of assessment.

name

text. The name used to reference this component from elsewhere in the document.

styleNumber

integer. Default value: 1. The style number used to select this component's visual styling from the available style definitions.

Properties for <matchesPattern name="m">

Highlighted (9)
$m.allowImplicitIdentities

boolean. Whether to allow implicit identity transformations when matching.

$m.allowPermutations

boolean. Whether to allow permutations of operands when matching.

$m.excludeMatches

mathList. Patterns whose matches are excluded.

$m.matchExpressionWithBlanks

boolean. Whether to allow expressions with blanks to match.

$m.numMatches

number. The number of matches found.

$m.patternMatches

[ math ]. The list of matched sub-expressions.

$m.requireNumericMatches

boolean. Whether numeric placeholders must match numbers.

$m.requireVariableMatches

boolean. Whether variable placeholders must match variables.

$m.value

boolean. Whether the input matches the pattern.

Other (17)
$m.allowedErrorInNumbers

number. Maximum allowed numeric error when comparing numbers.

$m.allowedErrorIsAbsolute

boolean. Whether allowedErrorInNumbers is interpreted as an absolute (rather than relative) tolerance.

$m.caseInsensitiveMatch

boolean. Whether text comparisons ignore letter case.

$m.disabled

boolean. Whether this component is disabled and cannot be interacted with.

$m.expandOnCompare

boolean. Whether to expand math expressions before comparing.

$m.fixed

boolean. Whether this component's value is fixed and cannot be modified.

$m.fixLocation

boolean. Whether this component's location is fixed (preventing it from being moved while still allowing other modifications).

$m.hidden

boolean. Whether this component is hidden from the rendered output.

$m.includeErrorInNumberExponents

boolean. Whether the allowed numeric error also applies to numbers in exponents.

$m.matchBlanks

boolean. Whether unfilled blanks in a math expression count as a match.

$m.matchByExactPositions

boolean. Whether to match list values by exact position rather than by content.

$m.numPeriodicSetMatchesRequired

integer. Number of consecutive elements of a periodic set required to count as a match.

$m.numSignErrorsMatched

number. Maximum number of sign errors that still count as a match.

$m.simplifyOnCompare

text. Level of simplification applied to math expressions before comparing.

$m.symbolicEquality

boolean. Whether children compared via this boolean use symbolic equality.

$m.text

text. The boolean rendered as a text string ("true" or "false", in the document's language).

$m.unorderedCompare

boolean. Whether order is ignored when comparing list-like values.

Common to all components (4)
$m.doenetML

text. The DoenetML source code that produced this component.

$m.hide

boolean. Whether to hide this component from the rendered output.

$m.isResponse

boolean. Whether this component is treated as a response for the purposes of assessment.

$m.styleNumber

integer. The style number used to select this component's visual styling from the available style definitions.

Examples

Example: Check the form of an equation

The <matchesPattern> component is used to check that the user has supplied an equation in the requested format. Here a, b and c are placeholders, each matching a term of whatever the student writes; only the y and the shape of the equation are fixed. Used without additional attributes as shown above, a rather liberal comparison occurs.

  • there must be three terms on one side, and none on the other
  • one side must contain only yy (RHS or LHS are acceptable)
  • the terms may be added or subtracted

These requirements may be tightened, if desired, by applying additional attributes to <matchesPattern>.

Example: Match a specific form of linear equation

In this example, the following conditions must be met:

  • a yy by itself on one side, and an xx on the other, and no other variables can be present
  • the xx value must have a numerical coefficient, or no coefficient
  • a constant term must be written, even if it is 00y = 3x + 0 is accepted but y = 3x is not
  • switching the order of terms on the right-hand side is acceptable

The attribute requireNumericMatches ensures that the user only enters numerical values for the parameters m and b (rather than an x, for example). Multiple <award> conditions are used to ensure that the case where xx does not have a coefficient are allowed.

Example: Require the same expression in more than one place

Naming u once and using it twice is what makes this work: a parameter that occurs more than once in the pattern has to match the same expression at every occurrence. So sin(x)^2 + cos(x)^2 matches and sin(x)^2 + cos(y)^2 does not.

A few things to know when using parameters:

  • The matches come back in the order the parameters were named, so the first entry of patternMatches is always what the first name in parameters matched. A parameter that does not occur in the pattern keeps its place in that list and reports a blank, with a warning.
  • A name listed in parameters is a placeholder at every place it occurs in the pattern, so it cannot also be matched literally there. Choose names that are not the ones you want matched exactly.
  • A parameter must be a variable name — a plain symbol such as u, or a subscripted one such as x_1 — the same names every other attribute that takes a list of variables accepts. Anything else, such as a number or a compound expression, is ignored with a warning. A function name counts, and is a useful case in its own right: pattern="f(x) + f(y)" parameters="f" matches sin(x) + sin(y) but not sin(x) + cos(y).

Blanks as placeholders

If parameters is left off, then blanks — written ( ) — are the placeholders instead: pattern="y = () + () + ()" behaves like the first example above. Each blank matches independently of the others, so there is no way to say “and the same expression here,” which is what parameters is for.

Specifying parameters turns this off: a ( ) is then matched as a literal blank, and a pattern containing one only matches an expression that has a blank of its own, which additionally requires matchExpressionWithBlanks.