« Previous 1 2
Regular expressions and metacharacters in PowerShell
Patterns
Checking Input
Along with the metacharacters that compare a string at the beginning ^
and at the end $
, you can now show a slightly more practical example in the form of querying an input string:
> $Input= Read-Host "Please enter three digits followed by at least 4 but less than 8 letters!"
Once the user has entered a value, you can check the input for correctness with the following regular expression (Figure 2):
> $input -match "^\d{3}\w{4,8}$"
If you still think this is very cryptic, simply try it out. This type of query is certainly not suitable for checking a password; however, it can be used to change incorrect input in shell scripts.
This example shows that the curly brackets can be used to check for a specific number of matches. In contrast, with square brackets []
, you can compare a specific range of characters. If you specify several characters within the brackets, the condition is correct if at least one of those characters is found in the object examined:
> $Teststring ='What is it To you, dude?' > $Teststring -match "[wy]" True
I have already mentioned that the shell cancels the comparison as soon as the first match is found, but it is also possible to specify a span of the alphabet within which (in this case) the sought-after letter should come (Figure 3):
> $Teststring -match "[p-t]" True ** $matches Name ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** Value 0 ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** t
This command finds the first character between p
and t
inclusive. Here, t
is the first match. In other scripting languages, you can use regular expressions of this type to run case-sensitive comparisons (e.g., by specifying the area as [P-T]
). Unfortunately, in many discussions on the web, you will find false statements telling you that this approach will also work in PowerShell.
At the time of writing this article in February 2017 under version 5, as found in Windows 10 and Windows Server 2016, PowerShell could not make case-sensitive comparisons. Additionally, a brief test with the current alpha version 6.0.0.16 showed no change in this behavior. If you want to restrict your search to uppercase, you not only have to specify the area as [P-T]
, but you also have to use the previously mentioned -cmatch
operator.
Finally, I'll look at a special feature of metacharacters when used in PowerShell that has certainly contributed to the bad reputation of regular expressions. Some of these metacharacters have different meanings and thus different effects, depending on where they are used. One of these characters is the aforementioned circumflex accent ^
, which allows you to find matching patterns at the start of a string. However, if you use this character within square brackets, it negates the formulated search pattern. The call
> $Teststring='Hello678 again' > $Teststring -match "[^a-z]" True ** > $matches Name ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** Value 0 ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** 6
discovers that the first character in the string that is not a letter is the number 6 . To find all related numbers in the string, use the
> $Teststring -match "[^a-z]+" variant; the output in the <C>$matches<C> variable is then <I>678<I>.
Conclusions
If you take a close look at the use of regular expressions in the context of metacharacters in PowerShell scripts, you will quickly discover the enormous opportunities hiding there, despite some pitfalls. Moreover, the world of regex holds significantly more special characters and options than I could possibly introduce in a single article. The old programming adage also applies: The quickest way to learn is by trial and error.
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy ADMIN Magazine
Subscribe to our ADMIN Newsletters
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Most Popular
Support Our Work
ADMIN content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.