PowerShell: regex

psregex01

psregex02

cd C:\Sandbox\PowerShell
# this returns true
 $regex = [regex]"xxx"
 $regex.IsMatch("xxx")
# both the next match because {u?} means that u may occur, or may not occur
 $regex = [regex] "colou?r"
 $regex.IsMatch("color")
 $regex.IsMatch("colour")
# both match because { (i|e) ) means that those are mandatory alternatives at that character position
 $regex = [regex]"art(i|e)fact"
 $regex.IsMatch("artefact")
 $regex.IsMatch("artifact")

Running a regex on dates is far more involved. My advice would be to write a .net class/method, and or a date tryparse, with the challenging bit often being the locale and the many possible formats.

Removing blank lines (not strictly PowerShell)

[\n\r]+$

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s