I have been looking for a regular expression with Google for an hour or so now and can't seem to work this one out :(
If I have a number, say:
2345
and I want to find any other number with the same digits but in a different order, like this:
2345
For example, I match
3245
or 5432
(same digits but different order)
How would I write a regular expression for this?
I think it's very simple to achieve if you're OK with matching a number that doesn't use all of the digits. E.g. if you have a number 1234 and you accept a match with the number of 1111 to return TRUE;
Let me use
PHP
for an example as you haven't specified what language you use.Something similar will work in Perl as well.
You could do something like this to ensure the right characters and length
Ensuring they only exist once is trickier and why this is not suited to regexes
Regular expressions are not appropriate for this purpose. Here is a Perl script:
Output:
I don't think a regex is appropriate. So here is an idea that is faster than a regex for this situation:
EDIT: Java Code (I'm using Character for this example, not exactly Unicode friendly, but it's the idea that matters now):
This will also work for comparing letters or other character sequences, like "god" and "dog".
The simplest regular expression is just all 24 permutations added up via the or operator:
/2345|3245|5432|.../;
That said, you don't want to solve this with a regex if you can get away with it. A single pass through the two numbers as strings is probably better: 1. Check the string length of both strings - if they're different you're done. 2. Build a hash of all the digits from the number you're matching against. 3. Run through the digits in the number you're checking. If you hit a match in the hash, mark it as used. Keep going until you don't get an unused match in the hash or run out of items.
There is an "elegant" way to do it with a single regex:
will match the digits 2, 3, 4 and 5 in any order. All four are required.
Explanation:
(?:2()|3()|4()|5())
matches one of the numbers 2, 3, 4, or 5. The trick is now that the capturing parentheses match an empty string after matching a number (which always succeeds).{4}
requires that this happens four times.\1\2\3\4
then requires that all four backreferences have participated in the match - which they do if and only if each number has occurred once. Since\1\2\3\4
matches an empty string, it will always match as long as the previous condition is true.For five digits, you'd need
etc...
This will work in nearly any regex flavor except JavaScript.