View Full Version : Reg Exp VB.NET

11-13-2003, 04:07 PM
I am working on a application at work for a helpdesk. My code freeze is Friday and I am trying to add in some neato features

Right now I am working on formatting the text, I basically have all of the HTML tags being converted over to BIU and some others but I am struggling to do the following in a small amount of code:

Say I have a string with a URL in it

Dim str1 As String = "Look at the following page: http://theURL.com for more details"

using a generic reg exp to match any URL would look like this???( I think)

veRegEx = New Regex("http://\s[3,]")

but how would I go about matching it but do the following

Dim str1 As String = "Look at the following page: <a href='http://theURL.com' target='_blank'>http://theURL.com</a> for more details"

I am just lost for ideas since I really suck at reg. exp.

I know a really long way to do it without reg. exp. but that is not fun!

Little challange for you all!

11-14-2003, 02:32 PM
okay I almost got it

ticket_body = Regex.Replace(ticket_body, "(\bhttp[s]{0,1}://[^ ]+\b)", "<a href=""$0"">$0</a>")

The only problem is it will not stop at the end of the line and takes over tags too

Maybe someone can help me with that....

11-18-2003, 04:03 AM
Haven't had time to mess with it, but have you tried leaving out the word boundary since you're already using "not space", or vice versa? Just an idea.

11-18-2003, 04:20 AM
Ok, this works in javascript, may not be the best way but it tested ok:

<script type="text/javascript">
var str = "Look at the following page: http://theURL.com for more details.";
var regExp = /(https?:\/\/[^ ]*)/
alert(str.replace(regExp,"<a href=\"$1\">$1</a>"));
document.write(str.replace(regExp,"<a href=\"$1\">$1</a>"));
// -->

11-18-2003, 04:35 AM
The reason why is some of those other characters are counted as word boundaries. They don't match the regular expression definition of a "word character", which is either alphabetical, numeric, or an underscore ONLY, i.e.:




The second is just a shortcut to match word characters.

Since there are slashes, colons, decimals, quotes, etc. in a URL, they're going to match \b - which will return only a partial match (and to be honest I'm having a hard time figuring out a way to work it like that, since in order to exclude "non-URL" characters, you would have to include every character that doesn't belong in a particular spot in a URL, which breaks the golden rule of K.I.S.S.) :)

I think if you can just make sure you encode the url when necessary if there's a querystring involved, that should take care of any spaces, then that regex is good to go... and there might still be a better one already written. :)

Here's a pretty nifty article (and website) I just found:


(P.S. This looks like it might be a worthy link...). Despite what they say, regular expressions are pretty similar (and normally close or absolutely identical in syntax) in almost all languages, including C# and VB.NET.

11-18-2003, 04:49 AM
By the way that text looks and sounds awkward (although I'm aware that it's probably your client's choice of words):

"Look at the following page: http://theURL.com for more details."


P.S. Where's some of your new and improved programs Alien51? :)

11-18-2003, 02:33 PM
You can not see my programs,....they are all on the intranet

My latest creation is a helpdesk software that our company uses coded in .NET.

I figured out the code last friday, my mistake was I was looking for < when I should have been looking for breaks \n

That was my whole mistake, but I got it to work.

I am working on a database design tool in my soare time that wil allow a person to design a database with a psyical model so they can see all the primary keys and foreign keys and restraints.

I am trying to code this in .Net and XML for storage, bu I have been too busy at work to code it.

Another progect is an AI tic tac toe player that learns from playing games. You give it a few basic rules and it will play and memorize good and bad combinations. Hopefully after a long series of games it should know the best combinations.

I also work on some business software at my work. My job on that is to make the NEAT-O features that know one else can figure out how to code. Plus I do most of the JavaScript work.

(And I was hired as Application Support which I still do - fix bugs and user errors in 8+ applications)


11-18-2003, 07:45 PM

11-18-2003, 09:23 PM
My days are too short!