简体   繁体   中英

regular expression for links in string to exclude dot only at end of line

I created a regular expression that reads a string and transforms found url's into HTML links. I wanted to exclude the dot at the end of a line (containing the text link) but it also excludes the dot inside the text link (like in http://www.website.com/page.html .) The end dot here should be excluded but not the .html. This is my regex:

    $text = preg_replace("#(^|[\n  \"\'\(<;:,\*])((www|ftp)\.+[a-zA-Z0-9\-_]+\.[^ \"\'\t\n\r< \[\]\),>;:.\*]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $text);                        

How would one do that?

Thanx! Tom

Change your RegEx to this

\b((?#protocol)https?|ftp)://((?#domain)[-A-Z0-9.]+)((?#file)/[-A-Z0-9+&@#/%=~_|!:,.;]*)?((?#parameters)\?[A-Z0-9+&@#/%=~_|!:,.;]*)?

or this

\b((?:https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|$!:,.;]*[A-Z0-9+&@#/%=~_|$]*)\b

Explanation

"
\b                            # Assert position at a word boundary
(                             # Match the regular expression below and capture its match into backreference number 1
                                 # Match either the regular expression below (attempting the next alternative only if this one fails)
      http                          # Match the characters “http” literally
      s                             # Match the character “s” literally
         ?                             # Between zero and one times, as many times as possible, giving back as needed (greedy)
   |                             # Or match regular expression number 2 below (attempting the next alternative only if this one fails)
      ftp                           # Match the characters “ftp” literally
   |                             # Or match regular expression number 3 below (the entire group fails if this one fails to match)
      file                          # Match the characters “file” literally
)
://                           # Match the characters “://” literally
[-A-Z0-9+&@#/%?=~_|\$!:,.;]    # Match a single character present in the list below
                                 # The character “-”
                                 # A character in the range between “A” and “Z”
                                 # A character in the range between “0” and “9”
                                 # One of the characters “+&@#/%?=~_|\$!:,.;”
   *                             # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
[A-Z0-9+&@#/%=~_|\$]           # Match a single character present in the list below
                                 # A character in the range between “A” and “Z”
                                 # A character in the range between “0” and “9”
                                 # One of the characters “+&@#/%=~_|\$”
"

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM