Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert eregi to preg_match?

Tags:

regex

php

I am using a lib which uses

eregi($match="^http/[0-9]+\\.[0-9]+[ \t]+([0-9]+)[ \t]*(.*)\$",$line,$matches)

but as eregi is deprecated now, i want to convert above to preg_match. I tried it as below

preg_match($match="/^http/[0-9]+\\.[0-9]+[ \t]+([0-9]+)[ \t]*(.*)\$/i",$line,$matches)

but it throws an error saying Unknown modifier '[' in filename.php

any ideas how to resolve this issue?

Thanks

like image 850
user187580 Avatar asked Mar 23 '10 16:03

user187580


2 Answers

If you use / as the regex delimiter (ie. preg_match('/.../i', ...)), you need to escape any instances of / in your pattern or php will think it's referring to the end of the pattern.

You can also use a different character such as % as your delimiter:

preg_match('%^http/[0-9]+\.[0-9]+[ \t]+([0-9]+)[ \t]*(.*)$%i',$line,$matches)
like image 91
Daniel Vandersluis Avatar answered Oct 01 '22 02:10

Daniel Vandersluis


You need to escape the delimiters inside the regular expression (in this case the /):

"/^http\\/[0-9]+\\.[0-9]+[ \t]+([0-9]+)[ \t]*(.*)\$/i"

But you could also chose a different delimiter like ~:

"~^http/[0-9]+\\.[0-9]+[ \t]+([0-9]+)[ \t]*(.*)\$~i"
like image 20
Gumbo Avatar answered Oct 01 '22 00:10

Gumbo