‘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
[…] terrifying looking regex was based on SqlChow‘s example, with PS1 appended to the […]