Using verbal expressions to make regex easy in PowerShell


‘Regular Expressions (regex) are my favorite programming tool’

said no one ever.

When I first came across them, I was a bit intimidated as many of us would be. They looked like complete gibberish. But, due to the nature of the code I was writing, I had to learn (or at least understand) them. Parsing SQL*Server error log files or netbackup logs, is not easy in Perl without regular expressions.

After using them a few times you realize that they are basically a tool to find specific patterns within a given text. But, you hit barriers when you try to use them to capture complex patterns. For example, consider this regex pattern:

'^(?:http)(?:s)?(?:://)(?:www\.)?(?:[^\ ]*)$'

Well what does it do? Not very easy to figure out right? But, it is the regex pattern for a valid URL; true story.

This is where verbal expressions steps in. Verbal Expressions is an open-source project that aims to simplify the task of writing difficult, complex regular expressions by providing a means of specifying regular expression patterns using easily readable text.

What it means is that, we write ‘Four times X plus 3 equals seven’, and get ’4x+3=7′. That example, could be a little misleading so let try it again, a valid url would consist of http at the begining of the line, maybe s if you are accessing a secure site, then it will have ://, maybe www. if you are so inclined to type and anything but a space. The aim of verbal expressions is to let you type the above and get a regular expression that matches a URL which is this: this:‘^(?:http)(?:s)?(?:://)(?:www.)?(?:[^\ ])$’.

Now, we need not be afraid of regular expressions. :-)

How to start

It’s easy, just import the module.

Import-Module -Name Poverb

Note:This is currently a partial implementation only.

How To use it

Currently the output is only a string that has the regular expression you desire.

	Get-RegexFromVerbex -Verbex {
		startofline
		then "http"
		maybe "s"
		then "://"
		maybe "www."
		anythingbut " "
		endofline
	}

the output is:

	'^(?:http)(?:s)?(?:://)(?:www\.)?(?:[^\ ]*)$'

How do I get it

You can get it from github, PowerShellVerbalExpressions

Other implementations

You can view all implementations on VerbalExpressions.github.io

References

  1. Jim Hollenhorst – The 30 Minute Regex Tutorial
  2. VerbalExpressions.github.io
  3. Peder Søholt
  4. Karl Prosser for writing Invoke-Ternary filter
  5. Doug Finke
About these ads
About

By profession, I’m a SQL Server Database Administrator. I love to poke my nose into different corners and see how stuff looks in there. I keep looking for new things to do as my mind refuses to settle on one topic.

Tagged with: , , , , ,
Posted in Fun-Stuff, PowerShell
One comment on “Using verbal expressions to make regex easy in PowerShell
  1. […] terrifying looking regex was based on SqlChow‘s example, with PS1 appended to the […]

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

Follow

Get every new post delivered to your Inbox.

Join 88 other followers

%d bloggers like this: