-2

In PHP, I used the ereg() function to determine whether a specified string can possibly generated by an input regular expression. I want to display all the strings that could be possibly generated by the regular expression given. How could I do that?

This is my current code that prints the string if it can be generated by regular expression $reg. I want to make it more complex by displaying all possible strings that could be generated by the regex.

<?php
$reg = $_POST['regex']; 
if(isset($_POST['calc'])){
if (ereg ("$reg", "kkjjj", $st)) 
{   
for($i = 0; $i < count($st)-1; $i++)
    {
    echo "$st[$i]";
    }

} 

else 
{
  echo "String not valid";
}

}
?>
John
  • 55
  • 1
  • 4
  • 1
    What do you expect the output to be if the input is `a+`? – Mr. Llama Feb 08 '12 at 17:23
  • 4
    `ereg` functions are deprecated. Use `preg` functions instead. – DaveRandom Feb 08 '12 at 17:24
  • 2
    stop using ereg(), it's deprecated... start using preg_match() instead.... and there's no need to wrap variables in double quotes ("$reg") if there's nothing else in those quotes, just use the variables "as is".... then, try explaining what you want to do, with example inputs and expected outputs – Mark Baker Feb 08 '12 at 17:24
  • ereg is deprecated, consider using [preg_match](http://php.net/preg_match) instead – ianbarker Feb 08 '12 at 17:24
  • You mean, all possible strings in an array, or all possible strings in the world? A regex could have infinite matching strings, so it would be a problem impossible to solve – Davide Feb 08 '12 at 17:28
  • I would correct your terminology too, a Regex doesn't "generate" strings, it attempts to "match" them. – Brenton Alker Feb 08 '12 at 17:32

2 Answers2

1

ereg function is deprecated as of PHP 5.3.0, preg_match is better choice, and if you need to get all matches, use preg_match_all, this functions use perl-compatible regular expressions syntax, so you need to add some changes (add slashes at least, your expression is equivalent to '/kkjjjj/' in perl-compatible regexp)

Nayjest
  • 1,089
  • 8
  • 14
0

I think the only way would be to brute force it. ie. Generate every string and see if it matches.

Even if you limit the search space to short strings, this is more difficult than it sounds as regex can encompass all UTF-8 characters which increases the number of permutations significantly over the "usual" characters on the keyboard.

So, not really, Regex isn't really something that can be reversed.

Brenton Alker
  • 8,947
  • 3
  • 36
  • 37