Showing posts with label School. Show all posts
Showing posts with label School. Show all posts

Monday, February 7, 2011

Regex are AWESOME

So today in class we`ve seen regular expressions ( widely known as Regex ). They are expressions used to recognize if a piece of code follows a certain pattern. A regex is usually a string of character representing the expression. Regex'es are language agnostic, meaning they will work with whatever language you are using provided it uses the right libraries.

A regex looks like this : "^[a-z]\d[a-z][\s-]?\d[a-z]\d$" .
This particular regex is used to check a string to see if it represent a canadian postal code (they look like this H2Y 8D7 with a space, dash or nothing in the middle).

How does it works ? Like this : A regular expression is filled with special alphanumeric characters and symbols. How you place these characters determines how the target string will be evaluated. Rather like programming languages with keywords and expressions.

The first set of symbols is the [a-z] interval. An interval is used to represent any value comprised between those two. In this instance I mean any lowercase letter between a to z.

The second set of symbol is \d . \d is a pre-defined character. It means whatever numeric character. It is the same as [0-9] but shorter. Then is another a to z interval. These three expressions are used to check for the first three characters of the targeted string. The last three is checked the same way.

A simple problem we are faced is the middle space, or the absence of it. Indeed we have to check that the fourth character is a space, dash or nothing. Several operators are used to check occurrences of several characters.

  • * : The asterisk is used to check 0, 1 or several occurrences of the previous character. A* will return true to A, AA, AAA, AAAA and basically every A in the text.
  • + : The plus sign is used to check 1 or several occurrences of the previous character (or expression between brackets [ ] a "class" ).  H+ will return true to HH, HHH and every other series of two consecutive H. Not simply one though.
  • ? : The interrogation mark is used to check 0 or 1 occurrence of the previous class. It will test if a class is there or not. This what we need to test if there is a space, a dash or none.
The expression used to test this condition is [\s-]? . This checks if there is, or not, a white space or a dash. White space being represented by \s.

Lastly, you can see symbols in the first and last position of the regex. These are to make sure the value we check does not contain any other characters before and after the string. The ^ symbol is used at the beginning of an expression to check if the string *starts* with the tested expression and the $ is used at the end to check if it *ends* with the expression. Using both we make sure the tested string starts and ends with our pattern.

An example :
We want to check if a string is a Canadian permanent code. These codes are made of four letters and 8 numbers with spaces(or not) at the 5th and 8th position.

Regular Expression :
^[a-z]{4}[-\s]?\d{4}[-\s]?\d{4}$

Test strings : 
SIGH03129117
DUG712345673
SIGH 0312 9117
SIGH 03129117
SIGH0312 9117
1234DUGH9154

Using the expression above, we can see that the second and last expression does NOT fit and we did that only by checking it against a string of symbols. Much less hassle than going by letter by letter with a loop.

Here is the program I made in class today, it contains many good examples. Available with the .net framework 4, as are the majority of what I do anyway. Don't forget to grab the latest version.

EDIT : Testing a regex can be a pain in the ass. This is why I use a tool, its here if anyone would like to try it. It is an AWESOME regex testing tool and I recommended it to anyone interested. 

Saturday, April 17, 2010

Blog is NOT dead.

Like I said in an earlier post. I won't be posting a lot these days. I have a shitload of work to do for school and friends to take care of, so the little time I get to program stuff is spent in fruitless attempts to re-engineer my movement system. That or creating sprites or tiles. I'm hoping to be able to post a bit more in the coming weeks, I just need some new material to blog about. I'll find something new to implement in my game and work on it.


Until then, take care !

Thursday, February 25, 2010

Classes Overview

Today we had to use classes. I will try to explain briefly what classes are, without going into too many details, because this is a deep subject.

First off classes are concepts of "things". A thing can be pretty much anything you can interact with. Chairs, tables, cars and clothes are all classes. Objects are physical representation of those concepts ( or logical, in the case of computers ). The particular chair you are standing on is the physical representation of the "chair" concept. Chairs are things which have several legs and a plate on which you can sit. But the one you are sitting on right now may have a number of legs and style and paint, it is unique ( even more unique when you think about the atoms making your chair ). Class = Chairs and Object = [The one chair you're sitting on]. 

Creating a class is made that way:

Public Class Car  'The class definition

Dim typeOfCar as string 'This is called a Member, it is specifically a property of a class (i.e a variable )


Sub nameCar(byval newValue as string) 'This is another member, it is a procedure.
typfOfCar = newValue


End Sub

End Class


By inserting the following snipper we have now created a Class called Car, it is a very general and broad class right now. It has a single property called typeOfCar that defines what type of car we're talking about. Right now it has no value so it can be anything. It a concept. When we create it, we define the car. We give it a brand, a name, a maximum speed, number of doors and etc. This is called an instance of a car. And creating an instance of a class is as easy as creating any other variable.

Dim MyViper as New Car
MyViper.nameCar("My Big Car")

We now have a living representation of a "car", its a dodge viper and its name is "My Big Car". We have given it a name using our member by accesing it using the syntax Class_Name.Procedure_Name


As you see, an instance of a class is an object. And the class itself that defines the procedures and properties is a Class. By using Object Oriented Programming you can do many things functional programming cannot do. The code you make is a lot neater and well structured. OOP is a lot more complex than that but thats all I'm going to risk explaining else if make some error and give you bad knowledge. That is all for today, hope to see you next time !

The format I'll use

So like I said in the previous post, I'm a student in IT and I currently have only a single class in programming. And its boring and I pretty much know everything we're learning ( having failed said class the first time ) . But I thought that it would make a good revision to summarize here the result of my classes here as I go to them. I think and I'm sure several people might agree that being able to explain the concepts means that you fully understand the subject. So my next class is coming up later today and I'll be sure to write a post about it when I'm done. Until then:

Do Until schoolClass = State.Finished
        Wait()
Loop