Mathematica 9 is now available
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 2002

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

Search the Archive

RE: Pattern matching

  • To: mathgroup at
  • Subject: [mg33962] RE: [mg33912] Pattern matching
  • From: "DrBob" <majort at>
  • Date: Wed, 24 Apr 2002 01:22:55 -0400 (EDT)
  • Reply-to: <drbob at>
  • Sender: owner-wri-mathgroup at

Here's a simple solution that builds on the QMMatchQ you already found.

QMMatchQ[s_String, p_String] :=
  MatchQ[Characters[s], Characters[p] /. "?"->_ ]

listData={"18K0F3C--" , "2K40GXX--" , "400HGXX--" ,"5M00G1F--" , 
listTemplates={"???H?????" , "???K?????"};


Bobby Treat

-----Original Message-----
From: John Leary [mailto:leary at] 
To: mathgroup at
Subject: [mg33962] [mg33912] Pattern matching


Can you help me please - there must be a simple solution to this
but I can't find it.

 From a list of character strings and a list of templates,  I need to 
produce a list of all strings that match any of the templates.  For

listData={"18K0F3C--" , "2K40GXX--" , "400HGXX--" , "5M00G1F--" ,
listTemplates={"???H?????" , "???K?????"}

In the templates, ? is a wild-card that represents a single character.
The data strings contain only alpha-numeric characters and hyphens - no 
other characters.
There are no special requirements for the result:  duplication and
order are acceptable.

I searched the MathGroup archive and found a very useful function that
exactly what I want, but it works only on individual strings, not lists
strings (msg00051):

QMMatchQ[s_String, p_String] := MatchQ[Characters[s], Characters[p] /.
-> _ ]

I tried to use it in the following way, but the result is a list of the 
matching templates, not the matching strings :

QMMatchQ[s_String, p_String] := MatchQ[Characters[s], Characters[p] /.
-> _ ]
SetOptions[Intersection, SameTest -> (QMMatchQ[#1,#2]& )];

It ought to be a small step from there to the result that I need, but I 
can't find a simple solution.

One alternative approach would be a Do loop:


This works but seems to be very slow for large lists.  In the real case,

listData can be very large - up to 250,000 elements - and the Do loop 
approach doesn't seem to be optimum.

I would be very grateful for your help.


John Leary

  • Prev by Date: Re: Pattern matching
  • Next by Date: Re: Pattern matching
  • Previous by thread: Re: Pattern matching
  • Next by thread: Re: Pattern matching