Re:Pattern match exactly 0 or 1 times?

*To*: mathgroup at smc.vnet.net*Subject*: [mg26424] Re:[mg26383] Pattern match exactly 0 or 1 times?*From*: "Ersek, Ted R" <ErsekTR at navair.navy.mil>*Date*: Wed, 20 Dec 2000 00:21:45 -0500 (EST)*Sender*: owner-wri-mathgroup at wolfram.com

Gavin Scott wrote: ------------------------ Is there a way to specify a pattern which will match exactly zero or one times? I find myself wanting somethinglike the standard regular expression '?' operator, but can't see a way to do this with Mathematica's pattern features. The closest I've been able to come is something like: x___ /; x=="a" which will match zero or more copies of a specific value. I'd like to match no more than one though. <snip> ----------------------- One of the methods below should help. In[1]:= Default[f]="a"; f[x_.]:={x} {f[], f[3], f[3,4]} Out[2]= {{a}, {3}, f[3,4]} In[3]:= g[x_:"a"]:={x} {g[], g[3], g[3,4]} Out[5]= {{a}, {3}, g[3,4]} If you don't like one of the methods above you can of course have a separate definition for no arguments. In[6]:= h[]="a"; h[x_]:={x} {h[], h[3], h[3,4]} Out[8]= {a, {3}, h[3,4]} ------------------------ Regards, Ted Ersek Download Mathematica tips, tricks from http://www.verbeia.com/mathematica/tips/Tricks.html