Advertisement
zopper

URL detection

Nov 8th, 2012
2,611
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PCRE 0.85 KB | None | 0 0
  1. # This regexp (for php preg_replace) should match any URLs in text (beginning with protocol or by www.)
  2. # BUT only if they are not encapsulated by $ ($http://somewhere$ MUST NOT be matched).
  3. #
  4. # At first it is here splited to parts for easier reading
  5. # EDIT: Moved punctuation inside the outer parentheses
  6. #
  7. # ------- this is here for checking the $ sign at the beginning -------#
  8. (?<!\$)(?<!\$http:\/\/)(?<!\$https:\/\/)(?<!\$ftp:\/\/)(?<!\$ftps:\/\/)
  9.  
  10. #-- detect protocol or www ----#
  11. ((((https?|ftps?):\/\/)|(www\.))
  12.  
  13. # everything except spaces and so - I'm counting with IDN.
  14. # And strip punctuation from the end
  15. (\S+\.\S+)*[^\s,.?!])
  16.  
  17. # check if the $ is not at the end
  18. (?!\$)
  19.  
  20. # full regex without splitting
  21. (?<!\$)(?<!\$http:\/\/)(?<!\$https:\/\/)(?<!\$ftp:\/\/)(?<!\$ftps:\/\/)((((https?|ftps?):\/\/)|(www\.))(\S+\.\S+)*[^\s,.?!])(?!\$)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement