echo regular expression all values

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
applefarm
Forum Newbie
Posts: 2
Joined: Wed Jul 20, 2011 5:39 am

echo regular expression all values

Post 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.
applefarm
Forum Newbie
Posts: 2
Joined: Wed Jul 20, 2011 5:39 am

Re: echo regular expression all values

Post 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.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: echo regular expression all values

Post 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.
Post Reply