This is a generalization of a specific problem I am trying to solve.
Given a string of uniquely identifiable elements separated by a single space:
0 1 2 3 4
Without modifying the string, what regular expression will match this expression when any of the numbers are optional except one and the numbers are in order?
Examples of other valid strings:
2
0 4
1 4
0 1 2 3
0 1 3 4
0 1 2 3 4
Regular expressions might not be the best tool for this. It’s just about manageable for the example you gave if you can use lookaheads:
Rubular
Explanation:
To generalize it to fixed length strings all of equal length makes it slightly more difficult to read. For example for
ab ba cd de efit will look like this:Rubular
But generalizing it to words of varying length gets more messy, for example for
zero one two three fouryou can use this regular expression:Rubular