Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

Regular Expressions and PowerShell Part 2

A helpful tool on the way in this series is Robby Foust’s  RegEx Quick Reference in PoSHcode.org.

The function returns an Object, so you can easy format and filter it to your liking.

I like this view the most:

get-regex | Format-Table Sequence,meaning -GroupBy table –AutoSize

As having a quick regex reference around this one is

POSH> get-regex |? {$_.table -match 'Character classes'}| Format-Table Sequence,meaning -GroupBy table -AutoSize

   Table: Character classes and class-like constructs

Sequence Meaning
-------- -------
[...]    A single character listed or contained within a listed range.
[^...]   A single character not listed and not contained within a listed range.
.        Any character, except a line terminator (unless single-line mode, s).
\w       Word character.
\W       Non-word character.
\d       Digit.
\D       Non-digit.
\s       Whitespace character.
\S       Non-whitespace character.
\p{prop} Character contained by given Unicode block or property.
\P{prop} Character not contained by given Unicode block or property.

Note that I use the –Match operator in this example to filter only the Character Classes in a where (?) clause, a place using a RegEx can be handy.

let’s go for part 3 of the series, there is Some PowerShell mentioned but I wanted to add this,

you can use static methods from the .NET framework (system.regularBLOCKED EXPRESSION as in the VB.NET/C# examples using the [regex] typeacceloprator.

POSH> [regex]::IsMatch('12345','^\d{5}$')
True
POSH> [regex]::IsMatch('123456','^\d{5}$')
False

there was also was a question about linenumbers in the webcast, you can use select-string also for this that gives linenumbers (and can do context etc. and also can work with regexes

POSH> type computers.txt
localhost
foo
localhost
POSH> cat .\computers.txt | select-string 'foo'

foo

POSH> cat .\computers.txt | select-string 'foo' | format-list

IgnoreCase : True
LineNumber : 2
Line       : foo
Filename   : InputStream
Path       : InputStream
Pattern    : foo
Context    :
Matches    : {foo}

POSH> cat .\computers.txt | select-string 'foo' | ft line* -a

LineNumber Line
---------- ----
         2 foo

You can see that select string seems to return text but that is just the default formatter, you get back more info.

and then pick you just the info you want

Enjoy,

Greetings /\/\o\/\/

Published Tuesday, December 30, 2008 6:41 PM by MoW
Filed under: ,

Comments

# Interesting Finds: December 31, 2008

Wednesday, December 31, 2008 10:38 AM by Jason Haley

# re: Regular Expressions and PowerShell Part 2

Glad you liked the script!  :-)

Thanks,

- Robbie (http://blog.robbiefoust.com)

Wednesday, December 31, 2008 9:18 PM by rfoust

# PowerShell Regular Expressions

I somehow ended up agreeing to do a session on Regular Expressions at an upcoming PowerShell UG meeting

Monday, January 26, 2009 11:53 AM by Richard Siddaway's Blog
Anonymous comments are disabled