+ Reply to Thread
Results 1 to 5 of 5

Thread: Regular Expressions with URL's

  1. #1
    Barn Frequenter BLaaaaaaaaaarche will become famous soon enough BLaaaaaaaaaarche will become famous soon enough BLaaaaaaaaaarche's Avatar
    Join Date
    Mar 2008
    Posts
    188
    Rep Power
    5

    Regular Expressions with URL's

    Does anyone have an example of a regular expression that takes all hyperlinks and email addresses and converts them into links? The text displayed would be the same text as the hyperlink or email address, but it would be a link, either an <a href> or <a mailto> link, depending on which it is. The other thing I would want is for the link text displayed be limited in length. For instance, if the string was 200 characters long, I could tell the regular expression function to limit the length to 50 characters, and add a "..." to the end of the link, hiding 150 characters and showing the first 50. Any ideas? Here is what I have now but cannot seem to figure out how to implement the character length bit:

    Code:
    Function LinkURLs(tempTxt)
      	Dim regEx
      	Set regEx = New RegExp
      	regEx.Global = True
      	regEx.IgnoreCase = True
    
    
      	'Hyperlink Email Addresses
      	regEx.Pattern = "([_.a-z0-9-]+@[_.a-z0-9-]+\.[a-z]{2,3})"
      	tempTxt = regEx.Replace(tempTxt, "<a href=""mailto:$1"" class=""blackBold"">$1</a>")
    
    
      	'Hyperlink URL's
      	regEx.Pattern = "((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:@=.+?,##%&~-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])"
     	tempTxt = regEx.Replace(tempTxt, "<a href=""$1"" class=""blackBold"" target=""_blank"">$1</a>")                 
    
    
      	'Make <a href="www = <a href="http://www
      	tempTxt = Replace(tempTxt, "href=""www", "href=""http://www")
    
    
      	LinkURLs = tempTxt & vbCrLf
    End Function
    
    "You'll never be as perfect as BLaaaaaaaaarche."

  2. #2
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Well, seeing as you have the hard part figured out...why not just use the Left and Len functions to "trim" the text portion of the hyperlink tag and then append the three periods?
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  3. #3
    Barn Frequenter BLaaaaaaaaaarche will become famous soon enough BLaaaaaaaaaarche will become famous soon enough BLaaaaaaaaaarche's Avatar
    Join Date
    Mar 2008
    Posts
    188
    Rep Power
    5

    Because this line is giving me an issue:

    Code:
    tempTxt = regEx.Replace(tempTxt, "<a href=""$1"" class=""blackBold"" target=""_blank"">$1</a>")
    
    When I tried to add different functions, it gives me an error. I wouldn't be asking if I could easily solve it.
    "You'll never be as perfect as BLaaaaaaaaarche."

  4. #4
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    What have you tried? Where are you adding it??
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  5. #5
    Barn Frequenter BLaaaaaaaaaarche will become famous soon enough BLaaaaaaaaaarche will become famous soon enough BLaaaaaaaaaarche's Avatar
    Join Date
    Mar 2008
    Posts
    188
    Rep Power
    5

    Well, I have a function that takes a text string, and trims it down to the number of characters you specify:

    Code:
    tempTxt = regEx.Replace(tempTxt, "<a href=""$1"" class=""blackBold"" target=""_blank"">" & TrimString($1, 50) & "</a>")
    
    It gives me an 'Invalid Character' error where the opening bracket in the TrimString function starts.
    "You'll never be as perfect as BLaaaaaaaaarche."

+ Reply to Thread

Similar Threads

  1. Search Engine friendly URL's using classic .ASP
    By decbrad in forum ASP Development
    Replies: 4
    Last Post: December 22nd, 2008, 08:51 PM
  2. Writing Expressions
    By nboscaino in forum Microsoft Access
    Replies: 3
    Last Post: August 21st, 2008, 05:48 PM
  3. Regular Expressions in C#...
    By Lauramc in forum .NET Development
    Replies: 2
    Last Post: April 21st, 2008, 12:54 PM
  4. Common Regular Expressions
    By jmurrayhead in forum .NET Development
    Replies: 0
    Last Post: April 21st, 2008, 12:34 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO