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 2004

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

Search the Archive

Re: Extract substrings using metacharacters

  • To: mathgroup at
  • Subject: [mg48534] Re: Extract substrings using metacharacters
  • From: Paul Abbott <paul at>
  • Date: Fri, 4 Jun 2004 04:50:15 -0400 (EDT)
  • Organization: The University of Western Australia
  • References: <c99dks$k5f$> <c9ha3q$14l$> <c9k3h7$fcu$>
  • Sender: owner-wri-mathgroup at

In article <c9k3h7$fcu$1 at>,
 Marcus Stollsteimer <marcus314 at> wrote:

> Paul Abbott wrote:
> > Since string-matching is somewhat limited, here is another way using 
> > "ordinary" pattern-matching, replacing the meta-characters "*" and "@" 
> > by their corresponding symbolic patterns:
> > 
> >   StringPatternTake[str_String, pat_String] := 
> >    If[StringMatchQ[str, pat], Characters[str] /.    
> >       (Characters[pat] /. {"*" -> x___,  "@" -> x__} ) :>   
> >          StringJoin[x], str]
> Paul, thanks a lot!
> your solutions work perfectly, I especially like the second version,
> which has the advantage that I can use the same pattern string
> both for FileNames[] and StringPatternTake[].  :)
> What I don't understand is why you treat "*" and "@" differently,
> shouldn't they both match 0 or more times, therefore in both cases
> use x___ (3 underscores)?

No. Have a read of Section 2.8.3 of the Mathematica book, or go to

> And I suggest that in the case that there is no match at all,
> the result should be an empty string rather than the input string.


> For my case it is not necessary, but wouldn't it be nice to
> generalize this function for patterns with multiple metacharacters,
> like "a*b*" (which should return a list of matched substrings)?
> Also, the metacharacter for the literal "*" (\*) could be implemented.
> I'll think about it when I have some spare time, but I figure
> it's probably not that easy... :)

I expect that someone has already written general tools for such 
operations ...


Paul Abbott                                   Phone: +61 8 9380 2734
School of Physics, M013                         Fax: +61 8 9380 1014
The University of Western Australia      (CRICOS Provider No 00126G)         
35 Stirling Highway
Crawley WA 6009                      mailto:paul at 

  • Prev by Date: Re: Problem with the Derivative of a Arg-function
  • Next by Date: Re: Problem with transformation rules in Plot
  • Previous by thread: Re: Extract substrings using metacharacters
  • Next by thread: Re: Extract substrings using metacharacters