2

If I compile a regex such as:

pat = re.compile("/resource/([a-z]*)/")

Is it possible to get matching components of this pattern (e.g., "/resource/", "([a-z]*)", "/") or some equivalent without executing match, search, or some other regex call? I want to know how the pattern "breaks out".

For example, the object's groups attribute tells me there is 1 group. But not much more information.

3
  • Can you tell us more about what you're trying to accomplish? Commented May 27, 2016 at 17:26
  • It looks like you want to do something similar to this: docs.djangoproject.com/en/1.9/topics/http/urls/… . If you're not using Django, what are you using? There might be a similar feature in your framework. Otherwise maybe look at the source of one of the frameworks that does this or try to find a library. Commented May 27, 2016 at 18:18
  • Suppose this was a generic URL "/r/([a-z]*)/", it would match "/r/foo/", "/r/bar/", etc. An internal component knows nothing about URL but it knows fetch(ID1), fetch(ID2). I would like to create a function that can map a generic URL to the internal representation. For example: shimfunc("/r/([a-z]*)/", [ ("foo", ID1), ("bar", ID2)]) ==> URL pattern is "/r/", <group>, "/" ==> so generate/return { "/r/foo/": ID1, "/r/bar/": ID2 } to be used later. Commented May 27, 2016 at 18:18

1 Answer 1

2

Use regex101's debugger feature. You can see how your regex failed or succeeded step-by-step.

Example:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Wow I have been using this tool forever and didn''t know this existed. Super useful!

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.