Last active
February 16, 2026 15:06
-
-
Save HeyItsGilbert/df9baf0e92344662036f3f08283e4b99 to your computer and use it in GitHub Desktop.
Example of how you can build a complex regex on multiple lines with comments.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $pattern = @' | |
| ^ # Beginning of the line | |
| # Get the hostname | |
| (?<hostname> | |
| # We have a DC name in the first 3 to 5 character | |
| (?<datacenter>.{3,5}) | |
| - # Literal dash | |
| # etc etc | |
| ) | |
| $ # End of the line | |
| '@ | |
| $options = [Text.RegularExpressions.RegexOptions]'IgnorePatternWhitespace' | |
| $regexMatch = [regex]::Match($HostName, $pattern, $options) |
Author
Exactly. If you need a # you gotta escape it, but other then that it can help with readability.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oh that's neat, so all the literal white spaces get ignored thanks to the regex options, and I assume that also ignores new lines which is what lets you spread that across multiple lines? And
\sor\or\nwouldn't be ignored because they're explicitly part of the pattern?