Function Repository Resource:

ToExpressionMatched

Source Notebook

Interpret input and check that the resulting expression matches a specific pattern

Contributed by: Sjoerd Smit

ResourceFunction["ToExpressionMatched"][input,patt]

interprets input as an expression only if the unevaluated form matches patt.

ResourceFunction["ToExpressionMatched"][input,patt,form]

uses interpretation rules corresponding to the specified form.

ResourceFunction["ToExpressionMatched"][input,patt,form,h]

wraps the head h around the expression produced before evaluating it.

ResourceFunction["ToExpressionMatched"][input,patt,Automatic,h]

uses the default interpretation rules from ToExpression.

Details

ResourceFunction["ToExpressionMatched"] returns a Failure object if the interpretation of the input does not match the requested pattern.
ResourceFunction["ToExpressionMatched"] uses ToExpression[input,form,HoldComplete] to parse the input, and then checks that it returns something that matches HoldComplete[patt] before removing HoldComplete or replacing it with a new head h.
ResourceFunction["ToExpressionMatched"][input,patt,Automatic,h] will use InputForm for string inputs and StandardForm otherwise.

Examples

Basic Examples (3) 

Interpret a string, expecting an integer:

In[1]:=
ResourceFunction["ToExpressionMatched"]["123", _Integer]
Out[1]=

A failure is generated if the result does not match:

In[2]:=
ResourceFunction["ToExpressionMatched"]["123", _Real]
Out[2]=

The pattern is checked before evaluation, so this fails because the head of the interpreted input is Plus:

In[3]:=
ResourceFunction["ToExpressionMatched"]["1+2", _Integer]
Out[3]=

Check that the head is Plus:

In[4]:=
ResourceFunction["ToExpressionMatched"]["1+2", _Plus]
Out[4]=

Instead, check that the input is a sum of integers:

In[5]:=
ResourceFunction["ToExpressionMatched"]["1+2", HoldPattern[Plus[__Integer]]]
Out[5]=

If the input cannot be interpreted as a valid expression, ToExpressionMatched behaves similarly as ToExpression, but it generates a Failure object instead of $Failed:

In[6]:=
ResourceFunction["ToExpressionMatched"]["1+", _Integer]
Out[6]=

Scope (3) 

Interpret boxes:

In[7]:=
ResourceFunction["ToExpressionMatched"][
 RowBox[{"1", "+", "2"}], _Plus, StandardForm]
Out[7]=

Wrap a head around the expression before returning it:

In[8]:=
ResourceFunction["ToExpressionMatched"][
 RowBox[{"1", "+", "2"}], _Plus, StandardForm, Hold]
Out[8]=

No head will be applied if a failure is generated:

In[9]:=
ResourceFunction["ToExpressionMatched"]["1+1", _List, InputForm, Hold]
Out[9]=
In[10]:=
ResourceFunction["ToExpressionMatched"]["1+", _List, InputForm, Hold]
Out[10]=

Automatically determine the interpretation rules appropriate for the input:

In[11]:=
ResourceFunction["ToExpressionMatched"]["1+2", _Plus, Automatic, Hold]
Out[11]=
In[12]:=
ResourceFunction["ToExpressionMatched"][
 RowBox[{"1", "+", "2"}], _Plus, Automatic, Hold]
Out[12]=

Publisher

Sjoerd Smit

Version History

  • 1.0.0 – 19 October 2021

Related Resources

License Information