Let's say I have a String, and I want to check if it matches another String in any possible (random) order. For example, let's say I have a String s, and I want to check if it matches "ONE" in a random order (== one of the following: ENO|EON|NEO|NOE|OEN|ONE, but not EEE;EEN;EEO;etc.)
In a more general description: I'm looking for a regex to match a String in a random order, with only the given characters of that String (this means the String itself can contain duplicated character, like SEVEN, but should use exactly two E's in the random matching, and not more or less).
I know I could use a literal ENO|EON|NEO|NOE|OEN|ONE to match ONE, and the same for TWO through NINE, but this is way too long..
I also know I can match everything including duplicated characters like this [ONE]{3}, but then it will also incorrectly matches EEE, EEN, and such.
I know I could also just create a loop to generate all these different permutations, and append them together with a | delimiter. And I think that might also be a valid solution in this case. But if this is possible with a regex I'd like to know as well for my own knowledge. Oh, and even if it's with a loop I also don't want it to be too long (since I'm actually asking this because of a code-golf challenge, where you solve a certain challenge in as few bytes as possible).
Aucun commentaire:
Enregistrer un commentaire