Retain part of a regex for a -replace

Welcome Forums General PowerShell Q&A Retain part of a regex for a -replace

This topic contains 6 replies, has 3 voices, and was last updated by

 
Participant
3 months, 3 weeks ago.

  • Author
    Posts
  • #109571
    Pj

    Participant
    Points: 0
    Rank: Member

    Hopefully this sounds worse than it is.

    $PSItem -replace "\d{1,4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2},\d{1,3}\sSomeUniqueText
    

    I want to use the above string but only change "td" to td style='background-color:#FF8080' leaving the original data defined by \d{1,4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2},\d{1,3}\sSomeUniqueText as it is.

    Long story short I need the whole string to define where to change but only want to change a piece of it leaving the rest alone.

  • #109574
    Pj

    Participant
    Points: 0
    Rank: Member

    ok its not populating right, its removing the html tag characters

  • #109577
    Pj

    Participant
    Points: 0
    Rank: Member
    $PSItem -replace "td\d{1,4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2},\d{1,3}\sSomeUniqueText"
    

    just picture the td as having html tags on it i guess

  • #109579

    Participant
    Points: 878
    Helping Hand
    Rank: Major Contributor

    Not able to understand your question. What problem are you facing ?

    Note: When you have update t your own post before anyone replies, please do the update using the edit option in the same post.

  • #109582
    Pj

    Participant
    Points: 0
    Rank: Member

    the forum is removing the characters you get when you hold shift and press , or .. its not letting me put the whole string in here. is there an escape character for this forum

  • #109589
    Pj

    Participant
    Points: 0
    Rank: Member

    Well found a way to do what I was trying to express. here is what I did

    if ($PSItem -match "td\d{1,4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2},\d{1,3}\sSomeUniqueText) { 
    $PSItem -replace "td", "td style='background-color:#FF8080'" }Else {$_}
    

    the td is an html tag but it wont let me put the symbols on it in the forum

  • #109592

    Participant
    Points: 324
    Helping Hand
    Rank: Contributor

    @PJ – HTML or XML you need to use Gist

The topic ‘Retain part of a regex for a -replace’ is closed to new replies.