0

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*",""

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.