Page 1 of 1
echo regular expression all values
Posted: Wed Jul 20, 2011 5:43 am
by applefarm
Hi,
I have a regular expression, i want to echo all possible values that match that regular expression.
Which function suits that need?
Br.
Re: echo regular expression all values
Posted: Wed Jul 20, 2011 5:51 am
by applefarm
This function still doesn't help:
Code: Select all
array preg_grep ( string $pattern , array $input [, int $flags = 0 ] )
Returns the array consisting of the elements of the input array that match the given pattern.
http://www.php.net/manual/en/function.preg-grep.php
Because it requires for my to have input array of possible values that i must check against regular expression.
But i have only a regular expression, i don't know the possible values, i want my code to find out al lthe matching possible values itself.
Re: echo regular expression all values
Posted: Wed Jul 20, 2011 2:47 pm
by McInfo
There are two approaches that I can think of. Neither is very practical.
1. Generate strings and try to match them with the regex. For example, {"a", "b", "c", ..., "aa", "ab", ...} This would be considered a brute-force approach. Given the number of possible character combinations, the computer would be working until the end of time, or at least until it ran out of memory or the regex function failed.
2. Write a regex parser and try to intelligently combine ranges of characters. For example, if the pattern is "^\d[a-f]$", the parser would determine that the string should contain two characters: the first is a member of the set {"0", "1", "2", ..., "9"}, and the second is a member of the set {"a", "b", ..., "f"}. So, the ten members in the first set times six members in the second set gives you 60 possible combinations: {"0a", "0b", ..., "0f", "1a", "1b", ..., "9f"}.
If you add another token to the regex, the total number of combinations is multiplied by the number of character variations that that token represents. If you add a repetition quantifier, the number of combinations grows enormous. For example, the simple regex "^\d+$" can match {"0", "1", "2", ..., "9", "00", "01", ..., "00000", ..., "0000000000000000000", ...}. The final combination contains 65535 "9"s. The number of combinations is [10 + 10^2 + 10^3 + ... + 10^65535]. That is... a really big number.