# \-replace, regex and using data from the current entry.

**URL:** <https://forums.powershell.org/t/replace-regex-and-using-data-from-the-current-entry/4877>\
**Category:** PowerShell Help\
**Created:** [September 15, 2015, 12:01am UTC](https://forums.powershell.org/t/replace-regex-and-using-data-from-the-current-entry/4877 "2015-09-15T00:01:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![oliadams1981](https://avatars.discourse-cdn.com/v4/letter/o/8baadc/32.png) [@oliadams1981](https://forums.powershell.org/u/oliadams1981)\
**Post date:** [September 15, 2015, 12:01am UTC](https://forums.powershell.org/t/replace-regex-and-using-data-from-the-current-entry/4877/1 "2015-09-15T00:01:54Z")

</div>

I have two parts of code in a html variable I am trying to edit. The first part is to set all values of 0 to be have a green font colour. I have done this using the following code.

$Post = $Post.Replace(“html td tag”‘0’html td tag,"html td tagstyle=’ color: green’\>0html td tag")

What I am trying to do now is set the same code “any number over 0” to red. I have tried $Post = $Post -replace(“html td tag[1-9]html td tag”,“html td tagstyle=’ color: red\>$1html td tag”) but this does not seem to work. Is it not possible to use $1 and $1 in this way or am I just doing it wrong?

---

<div class="post-metadata">

**Author:** ![max-kozlov](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@max-kozlov](https://forums.powershell.org/u/max-kozlov)\
**Post date:** [September 15, 2015, 5:57pm UTC](https://forums.powershell.org/t/replace-regex-and-using-data-from-the-current-entry/4877/2 "2015-09-15T17:57:55Z")

</div>

you use $1 inside double quotes, you should escape it as `$1 or use single quotes

---

<div class="post-metadata">

**Author:** ![brian-b](https://avatars.discourse-cdn.com/v4/letter/b/ba9def/32.png) [@brian-b](https://forums.powershell.org/u/brian-b)\
**Post date:** [September 24, 2015, 7:29am UTC](https://forums.powershell.org/t/replace-regex-and-using-data-from-the-current-entry/4877/3 "2015-09-24T07:29:36Z")

</div>

```
$Post = $Post -replace("html td tag[1-9]html td tag","html td tagstyle=' color: red>$($1)html td tag")
```

If you wish to use a variable inside quoted text, preface it with $( ) with your variable including the $ again inside the brackets. So: $($1)

EDIT: i’m still not entirely sure what you’re trying to do… as I’m not sure what $1 is equal to. could you post the current text on one line. what you’d like the text to be as a result, and explain again what you need ?

Brian

---

<div class="post-metadata">

**Author:** ![curtis-smith](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/curtis-smith/32/121_2.png) [@curtis-smith](https://forums.powershell.org/u/curtis-smith)\
**Post date:** [September 24, 2015, 9:47am UTC](https://forums.powershell.org/t/replace-regex-and-using-data-from-the-current-entry/4877/4 "2015-09-24T09:47:20Z")

</div>

Oliver, the problem appears to be that you are using .Replace rather than -replace. The -replace operator uses RegEx

```
$Post = @"
html td tag'0'html td tag
html td tag'1'html td tag
html td tag'2'html td tag
html td tag'3'html td tag
"@

$Post

Write-host "----------------"

$Post = $Post.Replace("html td tag'0'html td tag","html td tagstyle='color: green'")
$Post

Write-host "----------------"

$Post = $Post.Replace("html td tag'[1-9]'html td tag","html td tagstyle='color: red'")
$Post

Write-host "----------------"

$Post = $Post -replace ("html td tag'[1-9]'html td tag","html td tagstyle='color: red'")
$Post
```

## **Results:** ` html td tag’0’html td tag html td tag’1’html td tag html td tag’2’html td tag html td tag’3’html td tag`
`
html td tagstyle=‘color: green’
html td tag’1’html td tag
html td tag’2’html td tag
html td tag’3’html td tag
html td tagstyle=‘color: green’
html td tag’1’html td tag
html td tag’2’html td tag
html td tag’3’html td tag
`

`html td tagstyle=‘color: green’
html td tagstyle=‘color: red’
html td tagstyle=‘color: red’
html td tagstyle=‘color: red’
`

---

<div class="post-metadata">

**Author:** ![max-kozlov](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@max-kozlov](https://forums.powershell.org/u/max-kozlov)\
**Post date:** [September 24, 2015, 5:19pm UTC](https://forums.powershell.org/t/replace-regex-and-using-data-from-the-current-entry/4877/5 "2015-09-24T17:19:35Z")

</div>

besides, for $1 usage it is need to select matching captures with **()**

$Post = $Post -replace(“html td tag**(**[1-9]**)** html td tag”,“html td tagstyle=’ color: red\> **`** $1html td tag”)

`
PS D:\> $post = 'html td tag1html td tag'
PS D:\> $Post -replace "html td tag([1-9])html td tag","html td tagstyle=' color: red>`$1html td tag"
html td tagstyle=' color: red>1html td tag
PS D:\> $post = 'html td tag4html td tag'
PS D:\> $Post -replace "html td tag([1-9])html td tag","html td tagstyle=' color: red>`$1html td tag"
html td tagstyle=' color: red>4html td tag
`

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:44pm UTC](https://forums.powershell.org/t/replace-regex-and-using-data-from-the-current-entry/4877/6 "2024-05-16T20:44:44Z")

</div>

Closed on May 16, 2024, 8:44pm UTC


