I have certain strings as given below
PS C:\Users> $S1="PS CUT (ZIP ONLY), ALWAYS chose for MARTIO"
PS C:\Users> $s2="ZINO-IAS CUT"
PS C:\Users> $s3="ZINO-IPS CUT 2"
PS C:\Users> $s4="ZINO-IAS CUT4"
PS C:\Users> $s5="ZINO-IPS"
I want to replace above strings from the word CUT itself as given below
PS
ZINO-IAS
ZINO-IPS2
ZINO-IAS4
ZINO-IPS
Plase not that if there is a number after word CUT we are not removing that.
i tried the following code but it doesn't worked
PS C:\Users> $s5 -replace "CUT\s*\w*",""
The regex you need is:
"\s*CUT\s*(\d+)?.*","`$1"
It checks for an optional number that comes after CUT and any spaces and uses that as the replacement (represented by $1). If the number doesn't exist, it replaces the entire match with an empty string.
Result:
[PS]> $s1 -replace "\s*CUT\s*(\d+)?.*","`$1"
PS
[PS]> $s2 -replace "\s*CUT\s*(\d+)?.*","`$1"
ZINO-IAS
[PS]> $s3 -replace "\s*CUT\s*(\d+)?.*","`$1"
ZINO-IPS2
[PS]> $s4 -replace "\s*CUT\s*(\d+)?.*","`$1"
ZINO-IAS4
[PS]> $s5 -replace "\s*CUT\s*(\d+)?.*","`$1"
ZINO-IPS
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments