Skip to content

Instantly share code, notes, and snippets.

@HeyItsGilbert
Last active February 16, 2026 15:06
Show Gist options
  • Select an option

  • Save HeyItsGilbert/df9baf0e92344662036f3f08283e4b99 to your computer and use it in GitHub Desktop.

Select an option

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.
$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)
@joshooaj
Copy link

joshooaj commented Oct 1, 2025

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 \s or \ or \n wouldn't be ignored because they're explicitly part of the pattern?

@HeyItsGilbert
Copy link
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