I understood the difference between .Replace()
and -replace
, but what are -replace
and [Regex]::Replace()
?
I tested the 2 following codes, but for me the results are the exactly the same.
I also referred to PowerShell Cookbook(O'reilly), and it says
([Regex] is) extremely advanced regular expression replacement
I want to know what [Regex]
can but -replace
can't.
$line = "Loosen the socket by turning it#counterclockwise."
$line = $line -Replace "([a-z])#([a-z])","`$1 `$2"
$line
# Loosen the socket by turning it counterclockwise.
$line.GetType()
# IsPublic IsSerial Name BaseType
# -------- -------- ---- --------
# True True String System.Object
$line2 = "Loosen the socket by turning it#counterclockwise."
$line2 = [Regex]::Replace($line3,"([a-z])#([a-z])","`$1 `$2")
$line2
# Loosen the socket by turning it counterclockwise.
$line2.GetType()
# IsPublic IsSerial Name BaseType
# -------- -------- ---- --------
# True True String System.Object
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…