Friday, April 17, 2009

Regex – Still a Lot to Learn…

I though I was pretty good with Regex’s  - but just realized that even if I use it a lot – there are still a lot of things to learn. And Regex (I’m using the .Net version) is very powerfull.

The other day, I was searching for some useful regex’s, as I wanted to test for invalid email domain – e.g. the positive ones should be matched and the rest should be trapped. During this search, I stumbled across RegexBuddy and I decided to spent the €30 to buy it. And that had really sped up my learning! After getting the grips for the interface, it is very useful. First of all, no more repetitious string –replace pattern from PowerShell. Now everything is shown in color – live – and you can trace things down and even build the thing using graphical bricks.

So what have I learned so far?

Replacing each space in the start of a string/line – e.g. replace leading spaces with   – is easy (now) -

$line -replace '(?<=(^ *)) ','&nbsp;'



 






Splitting a distinguished name – taking escaped characters into account – also simple -




# PowerShell V1
[regex]::split($dn,'(?<!\\),')
# PowerShell V2 CTP3
$dn -split '(?<!\\),'





And how about writing a CSV with another delimiter than comma in PowerShell V1. I did a Convert-TsvToCsv earlier and have also written Export-Tsv (not published). But it can be done so easy with a regex that I’m almost ashamed over all the work I have done -




function Export-DelimitedFile($file,$delimiter=";") {
$work=Join-Path $env:temp ($myinvocation.mycommand.name + $pid + ".temp")
$input | Export-Csv $work
(type $work) -replace '("[^"\r\n]*")?,(?![^",\r\n]*"$)',"`$1$delimiter" | out-file $file
}





Have so much regex fun!

No comments: