2

For example, let's say I have this regex:

(jp(e)?g|png)

This can very easily be shortened to this:

(jpe?|pn)g

They both will do exactly the same thing, but the second one is shorter.

Is there a tool that I can put regex into and it will return a shortened version, much like the example above?

If it matters, I'm talking about Python regex.

Thanks in advance.

4
  • 1
    It's actually not the same and the tool cannot know this. In the first example, (e) will be a capture group, but in the second it isn't. The same for the other parentheses: in example 1, the g is part of the capture group but in example 2 it isn't. Therefore, no, such a tool probably does not exist, because it would need to break things. – Thomas Weller Nov 21 '16 at 22:34
  • @ThomasWeller But surely these two regex will always match the same strings as each other? Having just one character preceding a question mark, even if that singular character is in its own capture group, will have the same result - no? Like, "(e)?" means "e" is optional, and "e?" also means "e" is optional. So putting "e" in its own capture group should have absolutely no effect, am I wrong? – HalfOfAKebab Nov 21 '16 at 22:45
  • 1
    As long as you do not make use of the groups, yes – Thomas Weller Nov 21 '16 at 23:16
  • 1
    Given the complexity of regex, I think that it is highly unlikely that anyone will have taken the trouble to code such a tool. There might be a few low hanging fruit, but anything of reasonable complexity would be very difficult to code. And, to what end? To make regex even more complicated to comprehend? “Optimize regular expressions? Now you have three problems” :-) en.wikiquote.org/wiki/Jamie_Zawinski – Mawg says reinstate Monica Nov 22 '16 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.