Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 657 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Quoting -replace & variables

#1
This is in response to my previous question:

[To see links please register here]


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?

[To see links please register here]

[To see links please register here]

Reply

#2
The '$1' is a regex backreference. It's created by the regex match, and it only exists within the context of that replace operation. It is not a powershell variable.

"$1" will be interpreted as a Powershell variable. If no variable called $1 exists, the replacement value will be null.
Reply

#3
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](

[To see links please register 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`.
Reply

#4
In your second line:

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

Powershell replaces the $1 before it gets to the regex replace operation, as others have stated. You can avoid that replacement by using a backtick, as in:

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


Thus, if you had a string in a variable $prefix which you wanted to include in the replacement string, you could use it in the double quotes like this:

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


Reply

#5
Since I cannot comment or upvote, David Rogers' answer worked for me. I needed to use both RegEx backreference as well as a Powershell variable in a RexEx replace.

I needed to understand what the backtick did before I implemented it, here is the explanation: backtick is Powershell's escape character.

My usecase

$new = "AAA"
"REPORT.TEST998.TXT" -Replace '^([^.]+)\.([^.]+)([^.]{3})\.', "`$1.`$2$new."

Result

REPORT.TESTAAA.TXT


**Alternatives**

Format string

"REPORT.TEST998.TXT" -Replace '^([^.]+)\.([^.]+)([^.]{3})\.', ('$1.$2{0}.' -f )

Comments



as per

[To see links please register here]

I'll probably use the format string method to avoid the use of backticks.
Reply

#6
Here's the powershell 7 version where you don't have to deal with a single quoted $1, with a script block as the second argument, replacing 'ab' with 'b':

```
'abc' -replace 'a(\w)', {$_.groups[1]}

bc
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through