Re: Extract substrings using metacharacters
- To: mathgroup at smc.vnet.net
- Subject: [mg48469] Re: Extract substrings using metacharacters
- From: Paul Abbott <paul at physics.uwa.edu.au>
- Date: Tue, 1 Jun 2004 03:02:45 -0400 (EDT)
- Organization: The University of Western Australia
- References: <c99dks$k5f$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
In article <c99dks$k5f$1 at smc.vnet.net>,
Marcus Stollsteimer <marcus314 at yahoo.com> wrote:
> I do not have much experience with Mathematica string functions,
> so this may be trivial:
>
> When I use a string pattern with metacharacters
> (like the patterns used for StringMatchQ[] or FileNames[]),
> is there a simple way to extract the substring that
> matches the metacharacters?
> (something similar to regular expressions and m// in perl?)
>
> I have a lot of similar but different file names that I
> read in with FileNames[pattern], and I would like to extract
> the different parts.
>
> At the moment I have the following workaround:
>
> files = {"data040523.dat","data040527.dat","data040528.dat"};
> Do[
> datestr = files[[i]] // StringDrop[#, StringLength["data"]]& //
> StringDrop[#, -StringLength[".dat"]]&;
> Print[datestr],
> {i, 1, Length[files]}
> ]
>
> I am looking for a function StringPatternTake[str,"data*.dat"]
> that works like StringTake, but not with the positions, but
> with patterns instead.
For your list of files
files = {"data040523.dat","data040527.dat","data040528.dat"};
you can delete a specific string as follows:
StringDelete[str_String, del_String] :=
First[StringDrop[str, #] & /@ StringPosition[str, del]]
It is better to use StringPosition than StringDrop and StringLength for
you don't need to assume the structure of your string.
Making this function Listable (avoiding the need for Do loops),
SetAttributes[StringDelete, Listable]
you can obtain your date strings as folows:
StringDelete[StringDelete[files, "data"], ".dat"]
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]
Make this function Listable:
SetAttributes[StringPatternTake, Listable]
Then the following works:
StringPatternTake[files,"data*.dat"]
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