PowerShell to remove text from a string

前端 未结 5 545
不思量自难忘°
不思量自难忘° 2020-12-06 09:59

What is the best way to remove all text in a string after a specific character? In my case "=" and after another character in my case a ,, but keep th

5条回答
  •  -上瘾入骨i
    2020-12-06 10:28

    This is really old, but I wanted to add my slight variation for anyone else who may stumble across this. Regular expressions are powerful things.

    To keep the text which falls between the equal sign and the comma:

    -replace "^.*?=(.*?),.*?$",'$1'
    

    This regular expression starts at the beginning of the line, wipes all characters until the first equal sign, captures every character until the next comma, then wipes every character until the end of the line. It then replaces the entire line with the capture group (anything within the parentheses). It will match any line that contains at least one equal sign followed by at least one comma. It is similar to the suggestion by Trix, but unlike that suggestion, this will not match lines which only contain either an equal sign or a comma, it must have both in order.

提交回复
热议问题