replace regex??

This topic contains 6 replies, has 5 voices, and was last updated by  Curtis Smith 1 month ago.

  • Author
    Posts
  • #82541

    tommls
    Participant

    Where may I find something that will help me create a proper regex for this??

    $tableFragment = $tableFragment -replace '[ ]',''

    Basically I need to replace (remove, really) all instances of the string from the $tableFragment string.

    The way I have it that I found on the Internet does not work!! 🙂 🙂

    Thank you, Tom

  • #82546

    Olaf Soyk
    Participant

    What exactly are you looking for? For literal square brackets or for spaces?

  • #82547

    Rick
    Participant

    You need to use the regex backslash literal expression in front of the brackets because brackets are used in expressions. Then use a | symbol between to grab either bracket.

    $tableFragment = $tableFragment -replace '\[|\]',''
    
  • #82549

    tommls
    Participant

    Thank you everyone, Olaf asked about what I need.

    The pre code in my first forum post above don't show the string.

    The string contains multiple occurrences of

    table (blank space) table — as an empty table, with the html brackets around it which I can NOT reproduce here
    — basically an empty table.

    I want to remove ONLY all the empty tables — replace is the only way to do it b/c the string is of different lengths each time it's generated, hence using a replace. Other tables are in the string and they contain data (rows, columns).

    Thank you, Tom

  • #82567

    Matt Bloomfield
    Participant

    I think this is what you're after. Hoping I've remembered correctly that a Gist link won't remove the angle brackets.

    \s* will match 0 or more occurrences of whitespace. Unfortunately, I can't get it working across multiple lines with -replace although the regex code to match it should just be \s*[\r\n]* (verified working on https://regex101.com/).

    • #82586

      tommls
      Participant

      Hello, thank you, that was perfect.

      HOW did you get the code to display in your post to make the brackets appear?????

      I used line 2, replacing 'foo' with " and it works like a charm.
      I added a 2nd line to remove/replace some other extraneous code too.

      Thank you, Tom

  • #82600

    Curtis Smith
    Participant

    Hi Tom, to post the HTML code he used a gist as discussed by Don Jones at the top of this forum in post https://powershell.org/forums/topic/how-to-format-code-in-the-forums/

You must be logged in to reply to this topic.