The Problem
There is no easy way to get a permutation with a regex.
- Permutation: Getting a word $$w=x_1…x_n$$ ("aabc") to another order, without changing number or kind of letters.
- Regex: Regular expression.
For verification:
- "Regex permutations without repetition" The answer creates JavaScript code instead of a regex, assuming this would be more simple.
- "How to find all permutations of a given word in a given text" – The answer doesn't use regexes either.
- "Regex to match all {1, 2, 3, 4} without repetition" – The answer uses regexes, but it's neither adaptable nor simple.
- This answer even claims: "A regular expression cannot do what you're asking for. It cannot generate permutations from a string".
The kind of solution I am searching for
It should have the form:
- »aabc« (or anything else you could use a opening and closing parentheses)
- (aabc)! (similar to (abc)? but with another symbol in the end)
- [aabc]! (similar to [abc]+ but with another symbol in the end)
Advantages of these solutions
They are:
- easy
- adaptable
- reusable
Why this should exist
- Regexes are a way to describe a grammar of a regular language. They have the full power to be any kind of regular language.
- Let's say, regular languages are powerful enough for permutations (proof below) – why is there no easy way to express this?
So my question is:
- (Why) Is my proof wrong?
- If it is right: Why is there no easy way to express permutations?
The proof
- Regular expressions are one way to note the grammar of a regular language. They can describe any regular languages grammar.
- Another way to describe any regular languages (that have a finite number of letters within their alphabet) grammar are non-deterministic Automatons (with a finite number of states).
Having a finite number of letters I can create this automaton: (Example. Formal: see below)
Grammar that accepts permutations of "abbc":
(sry for numbers on top, maybe someone knows how to make this part looking better)
s -> ah¹
s -> bh²
s -> ch³
h¹ -> bh¹¹
h¹ -> ch¹²
h² -> ah¹¹ (no typo! equivalence)
h² -> bh²²
h² -> ch²³
h³ -> ah¹²
h³ -> bh²³
h¹¹ -> bc
h¹¹ -> cb
h¹² -> bb
h²² -> ac
h²² -> ca
h²³ -> ab
h²³ -> ba
More formal: (using a finite-state-automaton but this could be made with grammar as well)
- A word q (with finite length) to which any permutation should reach an accepting state.
- X is the finite alphabet.
- Set of states S contains any order of letters up to the length of q. (So the size of S is finite.) Plus one state of "any longer word".
- state transition function d which takes a letter and moves on the state that corresponds to the now read part of the word.
- F is a set of that states that are exact permutations of q.
So it is possible to create a finite-state automaton for accepting permutations of a given word.
Moving on with the proof
So I have proven that regular languages have the power to check for permutations, haven't I?
So why is there no approach to reach this with Regexes? It's a useful functionality.
^(a()|a()|b()|c()){4}\2\3\4\5$
seems to work (see regex101.com/r/9URPpg/4/tests). $\endgroup$