A regex I have working in Ruby doesn't in PHP; what could the cause be?

Posted by Alex R on Stack Overflow See other posts from Stack Overflow or by Alex R
Published on 2009-06-29T21:32:52Z Indexed on 2010/04/13 23:03 UTC
Read the original article Hit count: 262

Filed under:
|
|

I do not know ruby. I am trying to use the following regex that was generated by ruby (namely by http://www.a-k-r.org/abnf/ running on the grammar given rfc1738) in php. It is failing to match in php, but it is successfully matching in ruby. Does anyone see what differences between php's and ruby's handling of regexes that might explain this discrepancy?

http:\/\/(?:(?:(?:(?:[0-9a-z]|[0-9a-z](?:[\x2d0-9a-z]?)*[0-9a-z])\x2e)?)*(?:[a-z]|[a-z](?:[\x2d0-9a-z]?)*[0-9a-z])|\d+\x2e\d+\x2e\d+\x2e\d+)(?::\d+)?(?:\/(?:(?:[!\x24'-\x2e0-9_a-z]|%[0-9a-f][0-9a-f]|[&:;=@])?)*(?:(?:\/(?:(?:[!\x24'-\x2e0-9_a-z]|%[0-9a-f][0-9a-f]|[&:;=@])?)*)?)*(?:\x3f(?:(?:[!\x24'-\x2e0-9_a-z]|%[0-9a-f][0-9a-f]|[&:;=@])?)*)?)?/i

Since you all love regexes so much, how about an alternate solution. Given the ABNF in an rfc, I want a way (in php) to check if an arbitrary string is in the grammar.

APG fails to compile on a 64-bit system,

VTC is not Free,

and I have not found any other such tools. I would also prefer not to use a regex, but it's the closest I've come to success.

© Stack Overflow or respective owner

Related posts about regex

Related posts about ruby