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

This topic contains 4 replies, has 4 voices, and was last updated by Profile photo of Max Kozlov Max Kozlov 1 year, 10 months ago.

  • Author
    Posts
  • #29761
    Profile photo of Oliver Adams
    Oliver Adams
    Participant

    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?

  • #29798
    Profile photo of Max Kozlov
    Max Kozlov
    Participant

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

  • #30075
    Profile photo of Brian B
    Brian B
    Participant
    $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

  • #30083
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    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'

  • #30103
    Profile photo of Max Kozlov
    Max Kozlov
    Participant

    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

You must be logged in to reply to this topic.