[Tutor] Using Regex to produce text

Lie Ryan lie.1296 at gmail.com
Wed Apr 28 22:36:18 CEST 2010


On 04/29/10 01:32, mhw at doctors.net.uk wrote:
> While some patterns are infinite, other's aren't (e.g. The example I gave). 

How should the regex engine know about that?

> Using a subset of Regex syntax to produce a set of strings has the 
> advantage of using a well understood and documented form, and if you
> could hook into the existing API, at minimal coding effort.

> In addition, it allows a nice symmetry between search and production of resource names.

String generation is generally simpler than string parsing. If the
pattern of the string you're generating is so complex that you need a
regex-powered name generator, it will probably be impossible to parse
that. Use string interpolation/formatting instead: '%s_%0s.txt' % (name,
num)

> I suspect it's not that easy, as I don't think we can get to the internals of
> the regex FSM. However, I thought it would be worth asking.

The problem is how you would define the "universe" set of characters. If
you had a '.', would you want alphanumeric only, all printable
characters, all ASCII (0-127) characters, all byte (0-255) character,
all Unicode characters? It's too ambiguous and if you say to follow what
regex is doing, then regex just happen to not be choosing the most
convenient default for pattern generators.



More information about the Tutor mailing list