Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7540711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:42:21+00:00 2026-05-30T07:42:21+00:00

This is in response to my previous question: PowerShell: -replace, regex and ($) dollar

  • 0

This is in response to my previous question:

PowerShell: -replace, regex and ($) dollar sign woes

My question is: why do these 2 lines of code have different output:

'abc' -replace 'a(\w)', '$1'
'abc' -replace 'a(\w)', "$1"

AND according to the 2 articles below, why doesn’t the variable ‘$1’ in single quotes get used as a literal string? Everything in single quotes should be treated as a literal text string, right?

http://www.computerperformance.co.uk/powershell/powershell_quotes.htm
http://blogs.msdn.com/b/powershell/archive/2006/07/15/variable-expansion-in-strings-and-herestrings.aspx

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T07:42:22+00:00Added an answer on May 30, 2026 at 7:42 am

    When you use single quotes you tell PowerShell to use a string literal meaning everything between the opening and closing quote is to be interpreted literally.

    When you use double quotes, PowerShell will interpret specific characters inside the double quotes.

    See get-help about_quoting_rules or click here.

    The dollar sign has a special meaning in regular expressions and in PowerShell. You want to use the single quotes if you intend the dollar sign to be used as the regular expression.

    In your example the regex a(\w) is matching the letter ‘a’ and then a word character captured in back reference #1. So when you replace with $1 you are replacing the matched text ab with back reference match b. So you get bc.

    In your second example with using double quotes PowerShell interprets "$1" as a string with the variable $1 inside. You don’t have a variable named $1 so it’s null. So the regex replaced ab with null which is why you only get c.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Based on the response to this question: Why does C++ have header files and
In response to a previous question, I received this helpful answer : for (var
this is a follow up question of my previous question So I have this
I have deleted my previous question and post this updated: I have a an
As a response to my previous question, I think I may have discovered which
In @mmalc's response to this question he states that In general you should not
This question and my answer below are mainly in response to an area of
this is part of my code which reads an http response. It's supposed to
This is an attempt to rephrase my previous question as a result of the
This is a follow up to a previous question that I had before about

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.