Regular Expressions in ... Visual Basic .NET?

A recent project required me to use Windows while I figure out how to write a driver for a USB-based stepper motor driver I am using. I decided to see how Visual Basic works in this new and glorified .NET version (my last experience was with VB version 6 and it was a pleasant one).

So I downloaded the free version (called Express) of Visual Basic 2005. I am doing so because this way any advice I can give to others may include the usual "you may want to use this software" without having to add "it will cost you so many dollars". Because most of the time I'm giving advice to my students it seems an ok approach to me.

The only "requirement" to use this software for an unlimited amount of time is to give away some personal info to Microsoft. I've found the deal ok this time.

The new version includes a browser window on the user interface that makes the loading time longer and I am sure I will be happier without it (maybe it can be easily disabled).

I was not happy to learn that some old traditions no work like the ancient Open command. And things like Line Input seem to have given way to more Java-like structures like the StreamReader which makes you wonder how many other things have changed on this transition to the .NET framework. My guess is that a lot and I guess that many programmers are not very happy.

However, some changes are good. I was doing a part of a small project where I needed to parse an SVG file. I am familiar with the string handling commands usually available in Basic dialects (Right, Left, Mid, Len, etc). However my current task seemed to be better done with perl and regular expressions. Just in case I googled to see if any support was available and I was pleased to see that VB.NET includes a nice support for such a feature (not yet sure how efficient it is).

Some dark magic is needed first, like adding Imports System.Text.RegularExpressions in your file to get access to the proper class names. The next sample of code shows you how to get the coordinates from lines like this "....M123.456 789.123...."

Dim re As New Regex("M(\d+\.\d+) (\d+\.\d+)")
Dim m As Match
For Each m In re.Matches(line)
lastx = Val(m.Groups(1).Value)
lasty = Val(m.Groups(2).Value)
Console.WriteLine(lastx & " " & lasty)
Next

Nice addition!!

Although I was not aware of it, some support for regular expressions was already available on VB 6.0

Comments

Popular posts from this blog

VFD control with Arduino using RS485 link

How to get sinusoidal s-curve for a stepper motor

Importing OpenSCAD designs into Onshape