Friday, 13 November 2009

Regular Expressions 101 Part Two

In the previous post in this series we looked at a simple example of a regular expression:



^\d*$



which restricts input to either none or any number of digits.



The table below outlines a few slightly more complex variations of this regular expression and in which instances they would be valid.


"yes" means that particular number of digits would be valid input.




















































Quantity of numerical characters+*{2}{2,3}
Regular Expression^\d+$^\d*$^\d{2}$^\d{2,3}$
0
yes

1yesyes

2yesyesyesyes
3yesyes
yes
4yesyes


So to break this down "+" means one or more instances of the "pattern" specified
by the regular expression is required; "*" means none or any quantity of the
pattern need to be present; "{2}" means the length must be 2 instances of the
specified pattern; {2,3} means there must be either 2 or 3 instances of the
pattern present.

Here's a list of some characters found in regular expressions and what they mean:


  • . - any character

  • ? - optional i.e. there can be either none of these or any other quantity

  • + - at least one or more

  • * - none or any quantity

Regular Expressions and Grouping

You can also specify groups of characters to be matched by a regular expression. For example:

([A-Z][a-z]) specifies that the pattern must start with a Capital letter followed by a lower case character.

Clear as mud eh ;)

Tuesday, 10 November 2009

Regular Expressions 101 Part One

Today I managed to get a straightforward explanation for something which has always looked like gobbledy-gook to me i.e. Regular Expressions. So I thought I would share what I've picked up so far and add more as and when I get my head round this stuff.

So here's an example regular expression:

^\d{2}$

This would restrict input to numeric values only and two characters maximum length.

^ = start
\d = numbers only
{2} = how many
$ = end

There is a very useful regular expression testing tool here

Let's say you want numbers only i.e. no numbers or only numbers.

^\d*$

Friday, 9 October 2009

ASP.NET 3.5 Validation(): element is not supported

I am currently in the process of breaking my 3.5 cherry, loving the MVC stuff btw. Anyhoo I came across this problem where VS 2008 wasn't recognising 3.5 Webforms Controls in a 3.5 Webforms App! Every control I tried to add gave this warning:

Validation(): {control type} element is not supported

Solution:

Remove the Reference to the 3.5 Webforms Controls from the Web.config file:

<controls>
<add tagprefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<add tagprefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
</add>

Then rebuild the project. Go figure!

Thanks to this guy: http://forums.asp.net/t/1383876.aspx for the post.

Friday, 16 January 2009

String Arrays

Let's say you've got a bunch of email addresses all in one long string like:

me@bla.com,bla@bla.com,123@bla.com,noob@bla.com,helloworld@bla.com

And you want to separate these into individual email addresses.

Here's how:

Dim emails As String = "me@bla.com,bla@bla.com,123@bla.com,noob@bla.com,helloworld@bla.com"
Dim cleanEmails As String()
Dim s As String
cleanEmails = Split(emails, ",")

For Each s In cleanEmails
Response.Write(s & "
")

Next

This will write to the webform as:

me@bla.com
bla@bla.com
123@bla.com
noob@bla.com
helloworld@bla.com

Thursday, 15 January 2009

Getting the difference between two dates

I've lost count of the number of times I've been asked to provide the difference between two dates and the equal number of times anything to do with dates as left mean tearing what's left of my hair out.

So here's a couple of useful things:

The TimeSpan Structure

Example Usage:

Get the difference in days between two dates:

Public Function GetDays(ByVal date1 As Date, ByVal date2 As Date) As Integer
Dim span As TimeSpan = date1 - date2
Return span.Days
End Function

Eg. Get number of days between now and 1st Jan 2000

GetDays(Date.Now, "01/01/00")

or alternatively we can use DateDiff

Public Function GetTheDays(ByVal date1 As Date, ByVal date2 As Date) As Integer
Return DateDiff(DateInterval.Day, date1, date2)
End Function

Friday, 26 September 2008

Email Address Validation Regular Expression

Stick this in your regular expression validator for validating an email address textbox:

ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"

Then Robert's your father's brother.

Monday, 28 July 2008

ConfigurationManager not recognized in console application

So you follow best practice advice and store your database connection string in your console applications app.config file. Good so far, except when you try to access the setting in code you get ConfigurationManager not recognized!

Solution Add a reference to System.ConfigurationManager in your project references.

E.g.

Public ReadOnly Property conString() As String
Get
Return ConfigurationManager.ConnectionStrings("your connection string Name").ConnectionString
End Get
End Property

Thursday, 24 July 2008

ASP.NET Not Working - XML Parsing Error: not well-formed

God bless this guy:

http://theshiva.us/technicalblog/archive/2007/03/26/fix-for-asp-net-xml-parsing-error-not-well-formed-line-number-1-column-2.aspx

He gives a no bs explanation of what to do if when you set up an ASP.NET web application on a server you see this:

XML Parsing Error: not well-formed etc....


ASP.NET 2.0 Framework

%Windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -i

ASP.NET 1.1 Framework

%Windir%\Microsoft.NET\Framework\v1.1.4322\aspnet_regiis.exe -i

Wednesday, 23 July 2008

Skin Up - Using Skin Files to set Appearance of controls.

OK here's what I've got so far.

  1. Create a new skin file and save it
  2. Design your control layout on a test page
  3. Paste the finished formatting code into your skin file
  4. Set the Theme attribute in the Page Directive to point at your new Skin.
  5. Use it and re-use it.
mf...

DaysInMonth - get the number of days in a month

' get number of days in current month
Dim days As Integer = Date.DaysInMonth(Now.Year, Now.Month)
Me.lblDays.Text = days