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