By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 802 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Expanding regexps

P: n/a
Hi,

Is there a way to "expand" simple regexps? Something along the
lines of:

#v+
rx = '(a|b)c?(d|f)'
expand_regexp(rx) ['ad', 'af', 'acd', 'acf', 'bd', 'bf', 'bcd', 'bcf']

#v-

Cheers,

// Klaus

--<> unselfish actions pay back better

Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Klaus Alexander Seistrup wrote:
Is there a way to "expand" simple regexps? Something along the
lines of:

#v+
rx = '(a|b)c?(d|f)'
expand_regexp(rx) ['ad', 'af', 'acd', 'acf', 'bd', 'bf', 'bcd', 'bcf']


#v-


hint:

import sre_parse
print sre_parse.parse(rx)

the rest is just a small matter of programming.

</F>


Jul 18 '05 #2

P: n/a
Fredrik Lundh wrote:
Is there a way to "expand" simple regexps?
hint:

import sre_parse
print sre_parse.parse(rx)

the rest is just a small matter of programming.


I'm not sure it's just a small matter of programming for me, as I
will have to deduce what the notation means (is there a sre_parse
tutorial somewhere?), but thanks for the hint anyway, I'll try and
see if I can make something useful out of the output.
// Klaus

--<> unselfish actions pay back better

Jul 18 '05 #3

P: n/a
>> import sre_parse
print sre_parse.parse(rx)

the rest is just a small matter of programming.


Klaus> I'm not sure it's just a small matter of programming for me...

http://wombat.doc.ic.ac.uk/foldoc/foldoc.cgi?SMOP

Skip

Jul 18 '05 #4

P: n/a
Hi,
Is there a way to "expand" simple regexps? Something along the
lines of:


What would you expect to be the result of this rex:

r'a*'

?

The language accepted by this is of unlimited size - where do you stop?
Besides, whatever you do, its a matter of non-deterministic programming,
which can be accomplished by using backtracking - but for the price of
exponential time.

Regards,

Diez
Jul 18 '05 #5

P: n/a
Diez B. Roggisch wrote:
Is there a way to "expand" simple regexps? Something along
the lines of:
What would you expect to be the result of this rex:

r'a*'

?

The language accepted by this is of unlimited size - where do
you stop?


That's why I wrote "simple regexps", I'm fully aware that it would
be very difficult to expand all possible patterns - and that's not
what I need.

But to answer your question, even though it was probably rhetorical,
sre_parse looks at it from this angle:

#v+
import sre_parse
sre_parse.parse(r'a*') [('max_repeat', (0, 65535, [('literal', 97)]))]

#v-

// Klaus

--<> unselfish actions pay back better

Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.