views:

218

answers:

2

Hi. I'm trying to find the proper regular expression to convert eregi($1,$2) to preg_match("/$1/i",$2)

i need to consider if there will be functions with () in it, and they may appear more then once. can anyone please provide the proper regular expression to do so ?

thanks

+1  A: 

You don't want to use a regular expression to parse code.

You want to use a parser.

Anon.
i don't mind using a parser, i just want to find a way to replace one function to another in my code. what kind of parser i can use and how?
ufk
ok.. so php as a tokenizer extension that converts php code to tokens and from that i can convert back to code. thanks
ufk
+1  A: 

Are you trying to modify your source code, since eregi is deprecated? This regex will do the trick:

$source= <<<STR
eregi(\$1, \$2);
eregi('hello', 'world');
STR;

$source2= preg_replace("/eregi\(['\"]*([^\'\"),]+)['\"]*,\s*['\"]*([^'\"),]+)['\"]*\)/", 'preg_match("/$1/i", "$2")', $source);

var_dump($source2);
pygorex1
unfortunately this regex doesn't work for the following line of code:` if (!eregi('Windows 9', php_uname())) {`
ufk