I was reading about using RegEx in a -replace
object here , but the solution and the problem aren't quite what I am facing.
If I have the following variable:
$FinalOutput = Warning123 for Item: XYZ. Cannot specify "specific" object. Ignoring. Warning123 for Item: ABC. Cannot specify "specific" object. Ignoring. Error Found: Sending error to output.
Then use a simple -replace
to change the variable from all-inline to individual lines:
$FinalOutput = ($FinalOutput -replace "Warning123", "`nWarning123")
I'm now stuck with:
Warning123 for Item: XYZ. Cannot specify "specific" object. Ignoring.
Warning123 for Item: ABC. Cannot specify "specific" object. Ignoring.
Error Found: Sending error to output.
My variable will always have warnings and I wish to remove those values that start with Warning123
. Almost as if I wrote $FinalOutput = ($FinalOutput -replace "Warning123*", "")
. However, wildcards do not work in the -replace
object. My end result should look like:
$FinalOutput
Error Found: Sending error to output.
Using RegEx , I figured I could capture the start of the string with my specific phrase and then infidelity remove the characters after that string, leaving me with nothing:
^.*?Warning123 for Item:(.*)
However, that doesn't seem to work either. Any ideas or help on this solution?
$pattern = [regex]"(Warning123).*(object.)"
$FinalOutput = 'Warning123 for Item: XYZ. Cannot specify "specific" object. Ignoring. Warning123 for Item: ABC. Cannot specify "specific" object. Ignoring. Error Found: Sending error to output.'
$FinalOutput = ($FinalOutput -replace $pattern, "")
Outputs: PS C:\\> $FinalOutput Ignoring. Error Found: Sending error to output.
PS C:\\> $FinalOutput Ignoring. Error Found: Sending error to output.
Try that!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.