Showing posts with label Regular Expressions. Show all posts
Showing posts with label Regular Expressions. Show all posts

2015/08/30

PowerShell - Remove special characters from a string using Regular Expression (Regex)

Some more string manipulations! Today I'd like to remove the special characters and only keep alphanumeric characters using Regular Expression (Regex).

You might be interested to check a previous article where I showed how to remove diacritics (accents) from some strings, see here: http://www.lazywinadmin.com/2015/05/powershell-remove-diacritics-accents.html


My goal is to be able to keep only any characters considered as letters and any numbers.
If you are familiar with Regex, you could do something simple as using the metacharacter \w or [a-z] type of things. It's great when you only work with english language but does not work when you have accents or diacritics with Latin languages for example.

Preview of the final solution

2014/09/28

PowerShell Tip - Escape Regex MetaCharacters


Last week I worked on a Scorch PowerShell script that is looking for duplicate Incident Requests inside SCSM by checking new incoming request and existing ticket already in the system.

One of the script step is to look for a match between two strings, something similar to the following:

$String1 = "Title:[PowerShell Rocks!]"
$String2 = "Title:[PowerShell Rocks!]"

$String1 -match $String2

Straight forward you would think! And at first I was surprised to see the result $FALSE ... yep...to "ass-u-me"...

2014/09/06

PowerShell - ConvertFrom-String and the TemplateFile parameter

I'm continuing to play with the new ConvertFrom-String cmdlet (available in the last WMF 5.0 September preview released yesterday) which make the parsing job really easy for simple or complex output.

This cmdlets supports two types of modes: Basic Delimited Parsing (See yesterday's post) and the Auto-Generated Example-Driven Parsing which I will cover in this post.

This Auto-Generated Example-Driven Parsing mode is based on the FlashExtract research work in Microsoft Research...

Important: This post is based on the September 2014 preview release of WMF 5.0. This is pre-release software, so this information may change.

2014/09/05

PowerShell - Playing with the new ConvertFrom-String cmdlet

In a previous post I talked about parsing NetStat.exe using PowerShell and some regex, It is a fun exercice but require some knowledge to figure out how the parsing should happen.

Today, It got way easier !! The PowerShell Team just released a new version of the WMF : v5 September preview ! And One of the coolest feature is the new ConvertFrom-String cmdlet.

EDIT (2014/10/02): See also my post about using ConvertFrom-String and the param -TemplateFile against Netstat.exe

Using the same example from my previous post, I will perform a simple parsing of netstat.exe -n and send the output to ConvertFrom-String.

Important: This post is based on the September 2014 preview release of WMF 5.0. This is pre-release software, so this information may change.

2013/10/19

PowerShell - Get a SubString out of a String using RegEx

Last week one of my colleague asked me if I could help him with some Regular Expression (Regex) to select some text inside a String.

I don't work a lot with RegEx but when I do, I use tools like PowerRegex from Sapien, RegExr, the technet help for about_Regular_Expressions or RegExlib.com. And to be honest, most of the time I'm trying to avoid it...trying to find a solution the "PowerShell Way"  before trying with Regex...


Problem

So here is what he asked me
Out of the following string "OU=MTL1,OU=CORP,DC=FX,DC=LAB" (Which is a Distinguished Name), he wanted to get the name "MTL1", (SiteCode for Montreal).

2012/04/20

Video: Tobial Weltner – Regular Expressions in 5 Minutes

Video: Tobial Weltner – Regular Expressions in 5 Minutes:
One of my favorite lightning talks at the PowerShell Deep Dive in Frankfurt was the one that Tobias did on regular expressions.
Lightning Talks are super quick sessions at the conference in which anyone gets a chance to show something cool in 5 minutes. We tried doing these in Frankfurt and they had just amazing level of energy and got everyone really excited. I think we will do them again in San Diego in May.
Anyway, here’s the recording of the Regular Expressions talk by Tobias which I made in Frankfurt. Enjoy!

See more PowerShell Deep Dive recordings here.
This is a live recording from European TEC 2011 PowerShell Deep Dive conference. TEC US is just around the corner – April 29 – May 2, 2012 in San DiegoRegister now - this is the best PowerShell event you can find!