Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2000

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Pattern match exactly 0 or 1 times?

  • To: mathgroup at
  • Subject: [mg26396] Re: [mg26383] Pattern match exactly 0 or 1 times?
  • From: Ken Levasseur <Kenneth_Levasseur at>
  • Date: Wed, 20 Dec 2000 00:21:25 -0500 (EST)
  • References: <>
  • Sender: owner-wri-mathgroup at


I don't know exactly what you want to do but, for example, if you wanted
to square the first prime number in a list of  positive integers you
could do this:

{4, 6, 7, 42, 3, 9, 19} /.
  {{a___, b_, c___} :> {a, b^2, c} /; PrimeQ[b]}
{4, 6, 49, 42, 3, 9, 19}

In general, preceeding and following your desired pattern with
BlankNullSequence's will force Mathematica to only scan your data once.

Ken Levasseur
Mathematical Sciences
UMass Lowell

Three credit on-line Introduction to Mathematica starts at the end of

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.
> Is there anything like a general Regular Expression package for
> Mathematica anywhere?  I did a quick search and didn't find anything
> like this, so I'm wondering if my desire for such a thing simply
> indicates that there's something fundamental about Mathematica
> that I don't get yet which would eliminate the need for this.
> Thanks,
> G.

  • Prev by Date: RE: bug with Compile ?
  • Next by Date: Problem with the GaussianQuadratureWeights[n, a, b]
  • Previous by thread: Pattern match exactly 0 or 1 times?
  • Next by thread: Re:Pattern match exactly 0 or 1 times?