Link to home
Start Free TrialLog in
Avatar of bibi92
bibi92Flag for France

asked on

use variable on regex

Hello,

I try to use variable $_.fg_name  on regex but the result is []


$file = "C:\test\mv_ddl.sql"
$_.fg_name = "Primary"



(Get-Content $file) | ForEach-Object {$_ -replace [regex]::Escape("ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]"), "ALLOW_PAGE_LOCKS  = ON) ON \[$($_.fg_name))\]" -replace [regex]::Escape(") ON [PRIMARY]"), ") ON \[$($_.fg_name))\] TEXTIMAGE_ON [LOB]"} | Set-Content $file

How can I resolve, please?

Thanks

Regards
Avatar of oBdA
oBdA

$_ is the loop variable inside the ForEach, which is the string from the input file currently being processed.
Just use a regular variable (and please put code inside code style tags, see the buttons over the Edit field).
$file = "C:\test\mv_ddl.sql"
$fg_name = "Primary"

(Get-Content $file) | ForEach-Object {$_ -replace [regex]::Escape("ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]"), "ALLOW_PAGE_LOCKS  = ON) ON \[$($fg_name))\]" -replace [regex]::Escape(") ON [PRIMARY]"), ") ON \[$($fg_name))\] TEXTIMAGE_ON [LOB]"} | Set-Content $file

Open in new window

You need to get that variable another way, $_ is referring to the object piped in.  For example:

1,2,3 | %{ write-host $_ } 

Open in new window


So you need to reference the object which contains the property you want to use.  What object contains fg_name and can you post code where you are getting that object?
Avatar of bibi92

ASKER

I test Obda solution the result for the variable is  \[)\].

Thanks
Avatar of bibi92

ASKER

Thanks a lot
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bibi92

ASKER

thanks