Archive for the 'General' Category

08NovA good url regular expression? (repost)

I’m moving this post from http://geekswithblogs.net/casualjim/archive/2005/12/01/61722.aspx

I started out blogging on geeks with blogs but I can’t allow comments there anymore or I get too much spam, so I’m moving the post from there to this place.  Various people have contributed through the comments in the other blog post. So here I have better control over the spam and can open the comments again.

I have been looking for a good first layer of validating an url to see if it is valid.

For checking the format of the url it seems to me to be the most logical approach to use regular expressions. Up until now I always discarded them as being to “geeky”, meaning i don’t consider it my life’s biggest goal to be typing (/?[]\w) all day long (so why did i become a programmer, aaaah yes to make life easier for other people)

Anyway.. to find a good regular expression to that validates urls not url domains. One that doesn’t allow spaces in the domainname and where the domain can be suffixed with the port number.  Also I need support for the ~/ paths

This is what I came up with.. if somebody as a better idea… or finds a mistake please let me know.. Always happy to learn something new.

^(((ht|f)tps?\:\/\/)|~/|/)?([a-zA-Z]{1}([\w\-]+\.)+([\w]{2,5})(:[\d]{1,5})?)/?(\w+\.[\w]{3,4})?((\?\w+=\w+)?(&\w+=\w+)*)?

I was a bit quickly in using this regex. Simeon pilgrim indicated that the ftp urls won’t validate when you add a username and a password.

I don’t really need to validate ftp so I should have removed the ftp protocol from the list of choices.  I need this just to validate urls for weblinks and the link element in an rss feed.  When I need them for ftp I will post the ftp version.. but for now I don’t have time to spend on elaborating the regex.

Anyway here is the right one : ^(http(s?)\:\/\/|~/|/)?([a-zA-Z]{1}([\w\-]+\.)+([\w]{2,5}))(:[\d]{1,5})?/?(\w+\.[\w]{3,4})?((\?\w+=\w+)?(&\w+=\w+)*)?

A full url validation would include resolving names through dns or making a webrequest to the provided url to see if we get a 200 response. The only way to be sure is to test if it is there in my opinion.

Thanks Simeon.

And for those who really want the ftp validation : ^((ht|f)tp(s?)\:\/\/|~/|/)?([\w]+:\w+@)?([a-zA-Z]{1}([\w\-]+\.)+([\w]{2,5}))(:[\d]{1,5})?/?(\w+\.[\w]{3,4})?((\?\w+=\w+)?(&\w+=\w+)*)?

I am not sure about numbers in the username but I believe you can have a username of numbers alone.

Comments don’t seem to work on this blog engine.. so just send me a mail through the contact form. thanks

Two days later …

I discovered there is still a problem with my regular expressions… folders don’t get parsed.

I’ve solved the path issue, so now it should be finding all url’s

Expression:

^((ht|f)tp(s?)\:\/\/|~/|/)?([\w]+:\w+@)?([a-zA-Z]{1}([\w\-]+\.)+([\w]{2,5}))(:[\d]{1,5})?((/?\w+/)+|/?)(\w+\.[\w]{3,4})?((\?\w+=\w+)?(&\w+=\w+)*)?

Should parse the url below

http://hh-1hallo.msn.blabla.com:80800/test/test/test.aspx?dd=dd&id=dki

But not :

http://hh-1hallo. msn.blablabla.com:80800/test/test.aspx?dd=dd&id=dki

Update 29/11/2008:

Joe posted what seems to be a great regular expression in the comments

he tested it with the following urls:

http://www.google.com/search?q=good+url+regex&rls=com.microsoft:*&ie=UTF-8&oe=UTF-8&startIndex=&startPage=1

ftp://joe:password@ftp.filetransferprotocal.com

google.ru

https://some-url.com?query=&name=joe?filter=*.*#some_anchor

Expression:

^(?#Protocol)(?:(?:ht|f)tp(?:s?)\:\/\/|~/|/)?(?#Username:Password)(?:\w+:\w+@)?(?#Subdomains)(?:(?:[-\w]+\.)+(?#TopLevel Domains)(?:com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|travel|[a-z]{2}))(?#Port)(?::[\d]{1,5})?(?#Directories)(?:(?:(?:/(?:[-\w~!$+|.,=]|%[a-f\d]{2})+)+|/)+|\?|#)?(?#Query)(?:(?:\?(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)(?:&(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)*)*(?#Anchor)(?:#(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)?$

Update 8/11/2009:

Expression:

^(?#Protocol)(?:(?:ht|f)tp(?:s?)\:\/\/|~\/|\/)?(?#Username:Password)(?:\w+:\w+@)?(?#Subdomains)(?:(?:[-\w]+\.)+(?#TopLevel Domains)(?:com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|travel|[a-z]{2}))(?#Port)(?::[\d]{1,5})?(?#Directories)(?:(?:(?:\/(?:[-\w~!$+|.,=]|%[a-f\d]{2})+)+|\/)+|\?|#)?(?#Query)(?:(?:\?(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=?(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)(?:&(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=?(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)*)*(?#Anchor)(?:#(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)?$

There is a wave for this regex:

https://wave.google.com/wave/?pli=1#restored:wave:googlewave.com!w%252BsFbGJUukA

11MayJoined the lounge network

Today I received my invitation to the Lounge network. I just finished changing my homepage to include their ads in my side bar.
My reasons for joining the ad network are that on one hand I’ve been an ad-sense member for a really long time and although I’ve had ads on multiple sites it never actually made me any money. Eventually I gave up on putting those ads on my blog. So I’m not really doing it to make any money of this blog, but if it takes a small bite out of my hosting bill I’ll be a happy man :) .

The ads that are run by the lounge are .net centric and are a much better fit to the audience of this blog. It even may be helpful to somebody at times. 

Let’s see how this works out.

19MarPimp your command-line for git

I do development on both the Mac and windows. I prefer to use git as my source control these days and have done so for the past year or so.  Git is great, I love it. I love the ease of branching a lot too. I’ll often just branch of locally just to play around with an idea without affecting the master branch.

But having many branches can be confusing at times, especially in my case as I can only remember what I was doing for 5 seconds. So sometimes I mess up a perfectly good branch because of the confusion.

Continue reading ‘Pimp your command-line for git’

06JanA new year… some changes

With 2009 starting, started actually, it might be time to look ahead at what’s to come this year.

I hope your holidays were better than mine with my grandfather dying on Christmas eve, I wasn’t in much of a celebratory mood this year.

After having tried being a consultant for a while I have a serious hang-over from enterprise style of development. At least the dev style that only listens to what microsoft has to say and swears by their judgment under the motto: “You don’t get fired for buying Microsoft”. As if it wasn’t bad enough all the CRUD went through stored procs over linq-2-sql. When somebody there told me to copy/paste instead of taking a little bit more care I made up my mind and left the place. This leaves me at the start of this year without a project/job, and as it looks now it might not be the best position to be in with the crisis and all.

Another area that I desperately need to make some progress in is the IronRuby in Action book. So far I have 4 chapters completed and the one on Rails is about half-way there. Because I’m not making as much progress as I initially thought. This partly because I decided to turn my life upside down this year.
Now that I’ve finally found a good place to live and my personal life isn’t as messy as it used to be I’ve returned to writing.

More news on the IronRuby in Action front is that I’ve got a co-author now. His name is Michael Letterle and he has contributed to the IronRuby project.  Michael is very passionate about Ruby development and is currently working on the Silverlight chapter of the IronRuby in Action book.

As part of the Chapter on Rails I’ve built a twitter clone. In the wpf chapter I created a twitter client and to be ensure things continue to work both offline as online it seemed like a good idea to me to create the server side too.  The last couple of days I’ve been implementing this limited version and you can find it at http://codeplex.com/mocktwitter. Finishing this application is on my to-do list for this year for now it does a little bit more than it needs to for the samples from the WPF chapter to work.

More on the IronRuby subject. I’ve also created a DBI layer for ADO.NET that you can use in conjection with IronRuby to talk to ADO.NET data sources. I don’t know yet if I will base my activerecord adapters on this DBI layer or just with the providers immediately. I put a post up on how to get started and where to get the sources etc on rubydoes.net

I intend to spend some time on agdlr as well as on ironnails as well because ironnails has been a lot of fun to develop.

13JulMoving back to belgium

Tonight is my last night in New Zealand. Tomorrow I will fly back to Belgium to see what has changed in the 3 years that I’ve been away. I already know that my home city Antwerp has changed a lot because they were completely redoing the place when I left. I think it might still too soon for a reflective post on my experiences in New Zealand. What I can say is that I had a great time here and I made some great friendships with some extraordinary people. Depending on who you ask this is a good or bad thing but I’ll surely be back in a couple of years :)

Continue reading ‘Moving back to belgium’

08MarColor scheme for visual studio with resharper and Ruby in Steel support

This is certainly an exciting time for me. A release of dynamic silverlight that supports IronRuby, Asp.NET MVC that can be used to in conjunction with that.

I think that web applications and the way the web looks will severely change in the near future.

Anyway not only the people at Microsoft have been busy also the guys from sapphiresteel have been busy lately. They releases a really cool update to their Ruby In Steel product. A visual rails work bench and support for developing IronRuby apps with visual studio !

I finally got round to doing some installs/upgrades on my machine and played around with the color scheme until it suited me. I’ll post that here.

I use Resharper 4 as well as IronRuby and in Resharper I have code analysis turned on with color identifiers. So my scheme needs to support those.

I don’t like bright colors nor a totally black background, they have the same effect as a white background on me. I get migraines and can’t concentrate as long as I need to. (the latter may also be due to undiagnosed ADD).

So in C# with resharper the code looks like this:

CsharpColors

I do lot’s of webdevelopment but I haven’t tuned the colors for xaml yet. Will probably get round to doing that next weekend. This is how the html colors look (this is from rails html but html looks the same).

html

Then there still is ruby to show and a config file.

Here’s ruby:

rubyscheme

And lastly the config files:

webconfigscheme

Get DimmedInkResharperRubyInSteel (Visual Studio 2008)
Get DimmedInkResharperRubyInSteel (Visual Studio 2005)

27FebVisual studio gallery

This morning I found an announcement for the visual studio tools gallery. I went and had a look they have collected heaps of tools, some free most of them not into a website.

I promptly downloaded a couple of tools. One of them is something I recently discovered I need, local history. That is in between check-ins a file can also change but that isn’t recorded in source control so i can’t revert or view differences. Turns out that there is a project on codeplex that allows you to do just that. It’s called local history :)

 

Technorati Tags:

04FebOSX and Resharper get in eachothers way

And today I discovered that using unity severly interferes with my flow in visual studio.

I have ctrl-space mapped to quicksilver on mac osx which would mean I have to rebind either autocompletion or quicksilver, and it sure as hell won’t be my autocompletion. I think it will be too hard to unset that binding in my brain I’ve been using ctrl-space for about 10 years now, without even thinking about it. I even try to use it in Word but of course that doesn’t work ;)
The typing speed is also a little bit too sluggish for me to actually enjoy working in visual studio in unity mode. I decided to get back to a ruby project in textmate :)
I’ll try later on if it’s doable through full screen mode in fusion; and if that doesn’t work, I’ll have no other option than to boot into vista if I have to do .NET work.

There is another key missing from the mac keyboard, the insert key (and I have to admit about this one I’m definitely not that happy – what were they thinking??). So far the keyboard is the hardest to get used to. I mean those would probably be my most used shortcuts : ctrl – arrow, home, end, alt-insert and i now have to learn how to use different combinations and different ones in every program.

In other news, today i found myself using alt-C and alt-V on my windows keyboard when I wanted to copy paste. I think all the shortcuts have no right or wrong combination but it won’t be long before I master the ones on the mac that’s for sure.

I feel a lot better having that off my chest.

16JanBack to contracting

Yesterday I gave notice at Xero, my last day will be mid February (15/02).

It’s been fun working with the team of people at Xero, but I just want my own time back. I won’t go into more detail around the reasons for my leaving Xero but we part ways on friendly terms.

I’ve been getting more and more into Ruby again and I really like that language. So I’m mostly interested in jobs that will allow me to use those dynamic languages. I can now take some time off to finish the book I’m writing and to do some coding on a couple of things I have in mind.

Ideal places for me to work would be a place that practices Agile development et al. I’ll be going back to contracting probably unless somebody makes me a really great offer :)

14DecASP.NET MVC XsltViewEngine, patch submitted to the contrib project

I just submitted a patch to the mvc contrib project that contains the xslt view engine I wrote for Xero without any of the dependencies from our own libraries or commercial components.

The bulk of the work is done in a class called XmlResponseBuilder which builds the xml document that is going to be transformed by the xsl stylesheet. Almost all the rest of the code is there just to build up this document.

The implementation of IView and IViewFactory were the easy bits :)

To use the View engine you need to take the following steps:

1. Get the MVC Contrib project from google code

repository url: http://mvccontrib.googlecode.com/svn/trunk/

2. If the patch hasn’t been applied yet you can download it from: http://www.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=MVCContrib&DownloadId=23748

After downloading the patch you can apply it to the downloaded mvc contrib project.

3. I use a base controller to ensure that the correct data is passed to the view factory. This is the base controller class:

public abstract class XeroControllerBase : Controller

    {

        private readonly XsltViewData vData = new XsltViewData();

 

        protected IHttpSessionState Session

        {

            get { return ControllerContext.HttpContext.Session; }

        }

 

        public XeroControllerBase()

        {

            ViewFactory = new XsltViewFactory();  

        }

 

        #region Add datasource methods

 

        protected void AddDataSource(IXmlConvertible dataSource)

        {

            vData.DataSources.Add(new XslDataSource(dataSource));

        }

 

        protected void AddDataSource(IXmlConvertible dataSource, string rootName)

        {

            vData.DataSources.Add(new XslDataSource(rootName, dataSource));

        }

 

        #endregion

 

        #region Add variable methods

 

        protected void AddPageVar(string key, string value)

        {

            vData.PageVars.Add(key, value);

        }

 

        #endregion

 

        #region Add message methods

 

 

        protected void AddErrorMessage(string message)

        {

            vData.Messages.Add(new ErrorMessage(message));

        }

 

        protected void AddErrormessage(string message, string controlID)

        {

            vData.Messages.Add(new ErrorMessage(message, controlID));

        }

 

        protected void AddInfoMessage(string message)

        {

            vData.Messages.Add(new InfoMessage(message));

        }

 

        protected void AddInfoMessage(string message, string controlID)

        {

            vData.Messages.Add(new InfoMessage(message, controlID));

        }

 

        protected void AddAlertMessage(string message)

        {

            vData.Messages.Add(new AlertMessage(message));

        }

 

        protected void AddAlertMessage(string message, string controlID)

        {

            vData.Messages.Add(new AlertMessage(message, controlID));

        }

 

        protected void AddInfoHtmlMessage(string message)

        {

            vData.Messages.Add(new InfoHtmlMessage(message));

        }

 

        protected void AddInfoHtmlMessage(string message, string controlID)

        {

            vData.Messages.Add(new InfoHtmlMessage(message, controlID));

        }

 

        protected void AddErrorHtmlMessage(string message)

        {

            vData.Messages.Add(new ErrorHtmlMessage(message));

        }

 

        protected void AddErrorHtmlmessage(string message, string controlID)

        {

            vData.Messages.Add(new ErrorHtmlMessage(message, controlID));

        }

 

        protected void AddAlertHtmlMessage(string message)

        {

            vData.Messages.Add(new AlertHtmlMessage(message));

        }

 

        protected void AddAlertHtmlMessage(string message, string controlID)

        {

            vData.Messages.Add(new AlertHtmlMessage(message, controlID));

        }

 

        #endregion

 

        #region RenderView method hides

 

        protected new virtual void RenderView(string viewName)

        {

            RenderView(viewName, string.Empty, vData);

        }

 

        protected new void RenderView(string viewName, string masterName)

        {

            RenderView(viewName, string.Empty, vData);

        }

 

        protected new void RenderView(string viewName, object viewData)

        {

            RenderView(viewName, string.Empty, vData);

        }

 

        #endregion 

    }

 

del.icio.us Tags:
 
kick it on DotNetKicks.com

Recent Flickrs

    Blogroll

    Recent Listening

    Scrobbler