MathGroup Archive 2004

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

Search the Archive

Re: Extract substrings using metacharacters

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

In article <c9k3h7$fcu$1 at smc.vnet.net>,
 Marcus Stollsteimer <marcus314 at yahoo.com> 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

http://documents.wolfram.com/v5/TheMathematicaBook/PrinciplesOfMathematic
a/StringsAndCharacters/2.8.3.html

> 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.

Agreed.

> 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 ...

Cheers,
Paul

-- 
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 physics.uwa.edu.au 
AUSTRALIA                            http://physics.uwa.edu.au/~paul


  • 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