1

I have some difficulties learning regex in python. I want to parse my tornado web route configuration along with arguments into a request path string without handlers request.path method. For example, I have route with patterns like:

/entities/([0-9]+)

/product/([0-9]+/actions

The expected result combine with integer parameter (123) will be a string like:

/entities/123

/product/123/actions

How do I generate string based on that pattern?

Thank you very much in advance!

daimagine
  • 61
  • 2
  • 8
  • Could you edit your question to include some more example inputs and their expected outputs? Also, while you're at it, explain why you need to generate these strings? – Amal Murali Jul 02 '15 at 04:09
  • Generating string from regexp is not something available in the library of any language I am familiar with. However, if you search, there are some projects that do it. Basically, you need to generate the transition graph based on the regexp, then randomly walk it. Some modern "regexps" are not actually regular expressions, and it would be harder to make generators for them (e.g. any zero-width assertion complicates matters a lot). – Amadan Jul 02 '15 at 04:12
  • Thanks for your answer Amadan. Do you have any lib example that could save my problems written in python? – daimagine Jul 02 '15 at 05:38
  • A great way to play with python regex is with the [oneline web tool](http://pythex.org/) – meuh Jul 02 '15 at 14:24

1 Answers1

1

This might be a possible duplicate to:
Reversing a regular expression in Python
Generate a String that matches a RegEx in Python


Using the answer provided by @bjmc a solution works like this:

>>> import rstr
>>> intermediate = rstr.xeger(\d+)
>>> path = '/product/' + intermediate + '/actions'

Depending on how long you want your intermediate integer, you could replace the regex: \d{1,3}

Community
  • 1
  • 1
bro
  • 771
  • 3
  • 14