Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*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 smc.vnet.net
  • Subject: [mg48487] Re: Extract substrings using metacharacters
  • From: Marcus Stollsteimer <marcus314 at yahoo.com>
  • Date: Wed, 2 Jun 2004 04:21:51 -0400 (EDT)
  • References: <c99dks$k5f$1@smc.vnet.net> <c9ha3q$14l$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

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


StringPatternTake[str_String, pat_String] :=
    If[StringMatchQ[str, pat],
       Characters[str] /.
          (Characters[pat] /. {"*" -> x___, "@" -> x___} )
           :> StringJoin[x],
       ""]
SetAttributes[StringPatternTake, Listable]


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

Best regards,
Marcus

-- 
See, that's the thing about philosophy--
it's not all that functional once you get out of class. -- W. Allen


  • Prev by Date: Re: Eigensystem[] bug in Mathematica 5.0 (fixed in 5.0.1)
  • Next by Date: Re: checking accuracy with stepwise ode.
  • Previous by thread: Re: Extract substrings using metacharacters
  • Next by thread: Re: Extract substrings using metacharacters