2

My program takes in a regex for describing a set of devices. For example,

--device=dev{01,02}{nyc}.hukka.com 

should expand to dev01nyc.hukka.com and dev02nyc.hukka.com

How can I use the re module in Python to expand the user provided regex to complete strings that I can use? I am using Python 2.4.

3
  • 1
    This probably isn't possible. Regular expressions in general can accept languages that have an infinite amount of words, so RE libraries don't usually support trying to enumerate them. Commented Jan 11, 2012 at 21:00
  • What you've posted is not a regular expression. Commented Jan 11, 2012 at 21:10
  • You are essentially creating a new templating language. Be sure you really want that. unutbu does have the appropriate approach. Commented Jan 11, 2012 at 21:40

2 Answers 2

6

If we re.split on the braces, we get:

In [7]: re.split(r'\{(.*?)\}',userstring)
Out[7]: ['--device=dev', '01,02', '', 'nyc', '.hukka.com']

Every other item in the list came from inside braces, which we next need to split on commas:

In [8]: [ part.split(',') if i%2 else [part]  for i,part in enumerate(re.split(r'\{(.*?)\}',userstring)) ]
Out[8]: [['--device=dev'], ['01', '02'], [''], ['nyc'], ['.hukka.com']]

Now we can use itertools.product to enumerate the possibilities:

import re
import itertools

userstring = '--device=dev{01,02}{nyc}.hukka.com'

for x in itertools.product(*[ part.split(',') if i%2 else [part]  for i,part in
                              enumerate(re.split(r'\{(.*?)\}',userstring)) ]):
    print(''.join(x))

yields

--device=dev01nyc.hukka.com
--device=dev02nyc.hukka.com
Sign up to request clarification or add additional context in comments.

Comments

2

Simply by extract the first braces to a group and iterate over this group :

import re

user_arg = "dev{01,02}{nyc}.hukka.com"

regex = re.compile('dev{(?P<dev_id>[^}]*)}{(nyc)}.hukka.com')
result = regex.search(user_arg)

devices = []
for dev_id in result.group(1).split(',') :
    devices.append("dev%s%s.hukka.com" % (dev_id, result.group(2)))

print devices

That returns :

$ ['dev01nyc.hukka.com', 'dev02nyc.hukka.com']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.