1

I am trying to use this reg_expression to validate URLs:

  $pattern='/^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}
  \x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])|(%[\da-f]{2})|[!\$&\'\(\)\*\+,;=]|:)*@)?(((\d|[1-   
  9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.
  (\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|
  [\x00A0-\xD7FF\xF900-\xFDCF\xFDF0-\xFFEF])|(([a-z]|\d|[\x00A0-\xD7FF\xF900-\xFDCF\xFDF0- 
  \xFFEF])
  ([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])*([a-z]
  |\d|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])))\.)*(([a-z]|[\x{00A0}-\
  x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])|(([a-z]|[\x{00A0}-\x{D7FF}
  \x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])([a-z]|\d|-|\.|_|~|[\x{00A0}-\
  x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])*([a-z]|[\x{00A0}-\x{D7FF}\x{F900}-
  \x{FDCF}\x{FDF0}-\x{FFEF}])))\.?)
  (:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])
  |(%[\da-f]{2})|[!\$&\'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|
  [\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])|(%[\da-f]{2})|[!\$&\'\  
  (\)\*\+,;=]|:
  |@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}- 
  \x{FFEF}])|
  (%[\da-f]{2})|[!\$&\'\(\)\*\+,;=]|:|@)|[\x{E000}-\x{F8FF}]|\/|\?)*)?
  (#((([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-
  \x{FFEF}])|(%[\da-f]{2})|[!\$&\'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/u';

I got the following warning when trying to run preg_match: Compilation failed: range out of order in character class at offset 590 After a search I made it is obvious that there is an illegal character inside a character class...but it is very difficult to pinpoint the origin of the error.

So my question comes down to this: What offset 590 means? Obviously it refers to a position within the pattern but how am I going to find it.

5
  • There are several ways, for example the most simple one would be to cut the regex in half. Try the 2 regexes, one must sureley give an error. And so on until you find the bug. Ofcourse, you must cut it right. Otherwise, just use notepad++ or any editor which tells you the position of your cursor.
    – HamZa
    Oct 29, 2013 at 15:31
  • Load it into Emacs. Put your cursor at the beginning of the RE, then use M-590 C-f to go 590 characters into it.
    – Barmar
    Oct 29, 2013 at 15:33
  • count 590 characters in from the start of your regex. Or start chopping things down until you isolate the segment that's causing the problem.
    – Marc B
    Oct 29, 2013 at 15:35
  • 1
    Want's wrong with filter_var($var, FILTER_VALIDATE_URL)? Oct 29, 2013 at 15:39
  • I played a little with the code and a new offset was discovered...153, at this point here |[1-9]\d|1\d\d|2[0...153 is at 1 or somewhere around 1. DO you see anything wrong there? It is really frustrating to work with so huge regular expressions. The reason I am doing it?This regexp is what thw jquery validate plugin uses to validate url. I just want to use the same for server side purposes.Same pattern for client and server. But I am searching for alternatives cause I said it cumbersome. Oct 29, 2013 at 16:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.