Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make regex example commands use single-quoted strings #5417

Closed
mklement0 opened this issue Feb 5, 2020 · 0 comments · Fixed by #5420
Closed

Make regex example commands use single-quoted strings #5417

mklement0 opened this issue Feb 5, 2020 · 0 comments · Fixed by #5420
Assignees
Labels

Comments

@mklement0
Copy link
Contributor

@mklement0 mklement0 commented Feb 5, 2020

Unless PowerShell's string expansion is truly required, it's good habit to form and recommend to other to use single-quoted strings for:

  • regex operands (-match, -replace, ...)
  • the -replace operator's substitution operand.

The reason is that $ is a metacharacters in these constructs, and using "..." may result in confusion over who interprets $-prefixed tokens: is it PowerShell, up front, or is the regex engine that processes the resulting strings?

The linked topic and about_Comparison_Operators currently contain numerous example commands that needlessly use "..." rather than '...'.
I suggest changing them all to using single-quoting (except perhaps for one to demonstrate the pitfalls).

For instance, a current example command is:

# Should be:
#     'Gobble' -replace 'Gobble', '$& $&'
"Gobble" -replace "Gobble", "$& $&"

This happens to work, because & isn't a valid PowerShell variable identifier, so it leaves the $& alone, and the regex engine receives it as-is.

A somewhat contrived example to show that "..." can have unexpected side effects:

$1 = 'hi' # I've seen such variable names in the wild
'Gobble' -replace 'Go(bb)le', "$& $1"  # !! $1 is expanded *by PowerShell*

That is, instead of returning Gobble bb (which is what would happen with '...' quoting), Globble hi is returned, because $1 was recognized as a variable reference by PowerShell and therefore expanded.

If expansion is needed before the regex engine sees the result, the pass-through instances should be `-escaped; e.g.:

$var = 'hi'
'Gobble' -replace 'Go(bb)le', "`$& $var `$1"  # -> 'Gobble hi bb'

In general, I suggest using quoting consistently in all examples:

  • Always use '...' for verbatim content
  • Use "..." only if expansion is needed (or perhaps to make embedded ' chars. easier).

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

@mklement0 mklement0 changed the title Make the example commands use single-quoted strings Make regex example commands use single-quoted strings Feb 5, 2020
@chasewilson chasewilson self-assigned this Feb 5, 2020
sdwheeler pushed a commit that referenced this issue Feb 6, 2020
* Changes double quotes to single and adds note

* revert some changes

* reverts some changes

* reverts some changes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

3 participants
You can’t perform that action at this time.