Enumerating Regular Expressions

Mirco Wahab peace.is.our.profession at gmx.de
Tue May 9 09:46:49 EDT 2006


Hi blair.bethwaite

> I want a tool that can enumerate a regex, 
> with support for generating each string
> described by the regex in some predefined order.  

If you run the regex against some target
string, this is gonna be easy (but maybe
not what you want).

If you have the string 'Python' and run
a regex pattern \w+(greedy!) against it,
the result should read:

 match:Python
 match:Pytho
 match:Pyth
 match:Pyt
 match:Py
 match:P
 match:.ython
 match:.ytho
 match:.yth
 match:.yt
 match:.y
 match:..thon
 match:..tho
 match:..th
 match:..t
 match:...hon
 match:...ho
 match:...h
 match:....on
 match:....o
 match:.....n

These are then your strings resulting from "\w+"
This can be extracted by implanting code into
the regex, it generates the strings for you then.

I dont exaclty how to do this in Python,
but in the 'Gem-Liar' you would do a simple:

  $re = qr/
      \w+                       ## <-- insert your regex code here
      (?{print "match:",'.'x$-[0],$&,"\n"}) (?!) ## this prints it
  /x;

  $_ = "Python!";               ## against which string
  1 while /$re/g;               ## print them out


This should somehow work in Python
too, but my skills aren't up to the
task ;-)

Regards

M.



More information about the Python-list mailing list