<matchesPattern>
<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)
allowImplicitIdentitiesboolean. Default value: false. Whether to allow implicit identity transformations when matching.
allowPermutationsboolean. Default value: true. Whether to allow permutations of operands when matching.
excludeMatchesmathList. Patterns whose matches are excluded.
matchExpressionWithBlanksboolean. 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.
patternmath. Math expression pattern to match against.
requireNumericMatchesboolean. Default value: false. Whether numeric placeholders must match numbers.
requireVariableMatchesboolean. Default value: false. Whether variable placeholders must match variables.
Other (12)
allowedErrorInNumbersnumber. Default value: 0. Maximum allowed numeric error when comparing numbers.
allowedErrorIsAbsoluteboolean. Default value: false. Whether allowedErrorInNumbers is interpreted as an absolute (rather than relative) tolerance.
caseInsensitiveMatchboolean. Default value: false. Whether text comparisons ignore letter case.
expandOnCompareboolean. Default value: false. Whether to expand math expressions before comparing.
includeErrorInNumberExponentsboolean. Default value: false. Whether the allowed numeric error also applies to numbers in exponents.
matchBlanksboolean. Default value: false. Whether unfilled blanks in a math expression count as a match.
matchByExactPositionsboolean. Default value: false. Whether to match list values by exact position rather than by content.
numPeriodicSetMatchesRequiredinteger. Default value: 3. Number of consecutive elements of a periodic set required to count as a match.
numSignErrorsMatchednumber. Default value: 0. Maximum number of sign errors that still count as a match.
simplifyOnComparekeyword. Level of simplification applied to math expressions before comparing.
| Value | Description |
|---|---|
none (default) | No simplification is applied before comparing. |
full | Fully simplify both expressions before comparing. |
numbers | Simplify numeric subexpressions only, leaving symbolic structure intact. |
numbersPreserveOrder | Like numbers, but does not reorder commutative operands. |
normalizeOrder | Reorder commutative operands into a canonical form without simplifying values. |
symbolicEqualityboolean. Default value: false. Whether children compared via this boolean use symbolic equality.
unorderedCompareboolean. Default value: false. Whether order is ignored when comparing list-like values.
Common to all components (9)
copyreference. Create an independent copy of another component by reference. Enter a references a $name.
disabledboolean. Default value: false. Whether this component is disabled and cannot be interacted with.
extendreference. Extend another component by reference, inheriting its children and attributes. Enter a reference as $name.
fixedboolean. Default value: false. Whether this component's value is fixed and cannot be modified.
fixLocationboolean. Default value: false. Whether this component's location is fixed (preventing it from being moved while still allowing other modifications).
hideboolean. Default value: false. Whether to hide this component from the rendered output.
isResponseboolean. Default value: false. Whether this component is treated as a response for the purposes of assessment.
nametext. The name used to reference this component from elsewhere in the document.
styleNumberinteger. 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.allowImplicitIdentitiesboolean. Whether to allow implicit identity transformations when matching.
$m.allowPermutationsboolean. Whether to allow permutations of operands when matching.
$m.excludeMatchesmathList. Patterns whose matches are excluded.
$m.matchExpressionWithBlanksboolean. Whether to allow expressions with blanks to match.
$m.numMatchesnumber. The number of matches found.
$m.patternMatches[ math ]. The list of matched sub-expressions.
$m.requireNumericMatchesboolean. Whether numeric placeholders must match numbers.
$m.requireVariableMatchesboolean. Whether variable placeholders must match variables.
$m.valueboolean. Whether the input matches the pattern.
Other (17)
$m.allowedErrorInNumbersnumber. Maximum allowed numeric error when comparing numbers.
$m.allowedErrorIsAbsoluteboolean. Whether allowedErrorInNumbers is interpreted as an absolute (rather than relative) tolerance.
$m.caseInsensitiveMatchboolean. Whether text comparisons ignore letter case.
$m.disabledboolean. Whether this component is disabled and cannot be interacted with.
$m.expandOnCompareboolean. Whether to expand math expressions before comparing.
$m.fixedboolean. Whether this component's value is fixed and cannot be modified.
$m.fixLocationboolean. Whether this component's location is fixed (preventing it from being moved while still allowing other modifications).
$m.includeErrorInNumberExponentsboolean. Whether the allowed numeric error also applies to numbers in exponents.
$m.matchBlanksboolean. Whether unfilled blanks in a math expression count as a match.
$m.matchByExactPositionsboolean. Whether to match list values by exact position rather than by content.
$m.numPeriodicSetMatchesRequiredinteger. Number of consecutive elements of a periodic set required to count as a match.
$m.numSignErrorsMatchednumber. Maximum number of sign errors that still count as a match.
$m.simplifyOnComparetext. Level of simplification applied to math expressions before comparing.
$m.symbolicEqualityboolean. Whether children compared via this boolean use symbolic equality.
$m.texttext. The boolean rendered as a text string ("true" or "false", in the document's language).
$m.unorderedCompareboolean. Whether order is ignored when comparing list-like values.
Common to all components (4)
$m.doenetMLtext. The DoenetML source code that produced this component.
$m.hideboolean. Whether to hide this component from the rendered output.
$m.isResponseboolean. Whether this component is treated as a response for the purposes of assessment.
$m.styleNumberinteger. 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 (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 by itself on one side, and an on the other, and no other variables can be present
- the value must have a numerical coefficient, or no coefficient
- a constant term must be written, even if it is —
y = 3x + 0is accepted buty = 3xis 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 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
patternMatchesis always what the first name inparametersmatched. 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
parametersis 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 asx_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"matchessin(x) + sin(y)but notsin(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.