VB.NET String Manipulation, possibly regex

Relli

Weaksauce
Joined
Jan 16, 2001
Messages
98
I writing a program in vb.net that gets data from a database generated by another program. I get the data into the string just fine. Inside the string, there are a couple urls. I'm wanting to search the string and insert html link tags (<a href=""></a>) before and after the url. I haven't been able to come up with a good way to do it. I was thinking a regular expression might do it, but I'm not sure what methods to use or exactly sure how to use it. I'm not that good with regular expressions, but this is what I have:
Code:
http://[A-Za-z0-9._%-]*\n
What I think that does is looks for a string beginning with http:// followed by any number of upper, lower, digit characters or period, underscore, percent, or dash. All of that ending with a new line. The data I'm getting starts a new line after every url, so that was the only way I knew to end the search.

I guess I'll start there and make sure reg ex's are the best way to go and make sure my reg ex is correct. Please let me know if this is the best way to do it and if my reg ex is correct.
 
How's the URL stored in the DB? You say there are multiple URLs per record? That should probably be fixed on the DB side so you don't have this problem on the VB side.

The quickest way is to search through the string of URLS for your string and do a String.Insert at that index. I would suggest looking in the help files for "creating new strings" - there are other options like String.Format and so forth.
 
I am using 3rd party software that monitors multiple computer's event logs and stores the information in a db. I don't have any control over how that data is stored. I'm trying to get the data out of the database and build a summary page. Actually, that is all done. I'm just not sure how to find the end of a url, then wrap the link tags around it. Here is a sample of the database entry:
Code:
Application log generated Informational Event 58061 on server.namedds
For more information see [url]http://www.eventid.net/display.asp?eventid=58061&source=Backup[/url] Exec

Log: Application
Type: Informational
Event: 58061
Time: Sep 12 2006 11:00PM
Source: Backup Exec
Category: None
Username: N/A
Computer: SERVER
Description: Backup Exec Alert: Media Insert (Server: "SERVER") (Job: "Full2Tape") Please insert overwritable media into the drive. Overwritable media includes scratch, blank, and recyclable media. Please note that depending on the current Media Overwrite Protection setting, imported and allocated media may be overwritable as well. Consult the online help for more information on overwritable media. Respond Yes to acknowledge. Respond No to retry the operation. Respond Cancel to cancel the operation.



For more information, click the following link: 
[url]http://eventlookup.veritas.com/eventlookup/EventLookup.jhtml[/url]

I would like the urls to be links, but it isn't worth me trying to go into the software and change the way it writes the database. It looks like most things that do this, look for http then any characters followed by white space. Finding and replacing the http:// with <a href="http:// is easy to me. I just am not sure how to find the spot to place the end tag. Regular expressions seem like the way to go to me, but I'm not too familiar with them. String search and insert would work for the start, but how would I do it for the end tag? Any other suggestions?
 
I think that regular expressions are overkill for this problem.

I don't use VB. In C#, you'd use String.IndexOf() to find "http://". That would get the beginning of the URL, as an index into the string. You can then use String.IndexOfAny(), this time passing the starting point you got from IndexOf() and a list of whitespace characters. That'll get you the end of the URL. Then, you can do whatever you want to the string, now that you have the interesting part isolated.
 
Thanks, that makes perfect sense. I don't know why I didn't think of that. I think it just takes an outside set of eyes sometimes. I got my head into reg exp, then didn't see an easy alternative. Thanks a lot.
 
Back
Top